Connecting Tech Pros Worldwide Help | Site Map

removing .php extension from URL help

Member
 
Join Date: Nov 2007
Posts: 61
#1: Jun 9 '09
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
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Jun 9 '09

re: removing .php extension from URL help


what does your .htaccess code currently look like?
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#3: Jun 9 '09

re: removing .php extension from URL help


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.
n8kindt's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: Southern California
Posts: 221
#4: Jun 10 '09

re: removing .php extension from URL help


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
Member
 
Join Date: Nov 2007
Posts: 61
#5: Jun 10 '09

re: removing .php extension from URL help


Quote:

Originally Posted by Markus View Post

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.


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!
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#6: Jun 10 '09

re: removing .php extension from URL help


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'.
Reply

Tags
htaccess, remove extension, url


Similar Apache Web Server bytes