473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to remove .php extension from URL

Hi
I need to remove the .php extension using .htaccess from my URL.
I have tried different .htaccess code by searching in the net but none is fruitful..

I have the least idea about regular expressions. can anyone please help?

my URL is
http:///www.domainname.com/projectna...p/region/state

here region and state are dynamically changing... and i want to remove .php from filename.php
Jun 9 '09 #1
9 20005
Dormilich
8,658 Expert Mod 8TB
what does your .htaccess code currently look like?
Jun 9 '09 #2
Markus
6,050 Expert 4TB
Say your .htaccess file is in /projectname/. Try the following:

Expand|Select|Wrap|Line Numbers
  1. # Start the engine. Vroooom.
  2. RewriteEngine On
  3.  
  4. # This resolves paths to the current directory. (You may or may not need this).
  5. RewriteBase /projectname/
  6.  
  7. # And the rule.
  8. RewriteRule ^([a-zA-Z0-9]+)(/[a-zA-Z0-9/]+)?$ /$1\.php$2 [L]
  9.  
The (untested) rewrite basically takes everything after the RewriteBase, up to the first forward slash (if there is one) and stores everything in variables. We then rewrite the url, adding '.php' to the first capture ($1), appending the second capture ($2) to the URL too.

Let me know if this helps.
Jun 9 '09 #3
n8kindt
221 100+
you could also tell php to parse a file with your own custom file extension. for instance in your case you could do

http://www.domainname.com/projectname/file.name

by putting something like this into your .htaccess

Expand|Select|Wrap|Line Numbers
  1. #tell php to read all files with ".name" as the file extension
  2. AddHandler application/x-httpd-php5 .name
  3.  
that way it would look like the "dot" between file and name was on purpose and have nothing to do with the language your code is written in
Jun 10 '09 #4
@Markus

Thanks Markus for providing the code. But the snippet didn't work for me. when I removed the .php extension from the filename the page did not come up.
But I think I can try my hand with what you have provided.

Thank you very much!
Jun 10 '09 #5
Markus
6,050 Expert 4TB
It was untested (I was a little strained for time).

Anyway, try this one:

Expand|Select|Wrap|Line Numbers
  1. RewriteRule ^([a-zA-Z0-9]+)(/.*)?$ /$1\.php$2 [R]
Note: the [R] flag will rewrite the URL. That is, the URL will be visibly changed. Change it to [L] (or omit the flag altogether) if you want to URL to be 'masked'.
Jun 10 '09 #6
markus' first snippet worked for me (without the rewritebase part)
Oct 15 '10 #7
For Me Its Not Working Markus
Oct 16 '10 #8
hey try this..... It is tested and working for hiding the .php extension.......

Expand|Select|Wrap|Line Numbers
  1. RewriteEngine on
  2. RewriteCond %{REQUEST_FILENAME} !-d
  3. RewriteCond %{REQUEST_FILENAME}\.php -f
  4. RewriteRule ^(.*)$ $1.php
my id is ** Edit - Removed **
Nov 16 '10 #9
thankyou so much man!!!!!!! Its perfect what i as looking for...
Aug 19 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Gyger | last post by:
Hello, Three weeks ago, I have started to develop a binding extension for Qt and PHP 5. Now, I can display a dialog box containing some widgets like label, buttons and edit line. I have just...
1
by: lievemario | last post by:
Hey, I have generated following xml file (using perl,dbi), Now I would like to display this with the help of xsl such that I have the following result: A B (XXXX) (YYYY)
10
by: scott | last post by:
Below, I'm trying to remove the querystring name& value of "catID=12". I mananged to isolate the RESULTS part, but I need to be able to strip the querystring name and it's value, no matter if the...
102
by: tom fredriksen | last post by:
Hi I was doing a simple test of the speed of a "maths" operation and when I tested it I found that removing the loop that initialises the data array for the operation caused the whole program to...
3
by: shawn | last post by:
I am having a hard time finding an easy way to remove a reference to another .net project that I am no longer using (ASP 2.0 - this was an available option in 1.0). Other projects have a list of...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
0
by: robert | last post by:
Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: <definitions name="MaragatoService"...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
6
by: wilson | last post by:
i was trying to convert all images in a folder to another type and save the new images in a separate folder.for that i wrote a class and coded some part class ConvertImgs: def...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.