Connecting Tech Pros Worldwide Help | Site Map

removing .php extension from URL help

  #1  
Old June 9th, 2009, 07:45 AM
Member
 
Join Date: Nov 2007
Posts: 58
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
  #2  
Old June 9th, 2009, 08:02 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: removing .php extension from URL help


what does your .htaccess code currently look like?
  #3  
Old June 9th, 2009, 11:23 AM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,858
Provided Answers: 9

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.
  #4  
Old June 10th, 2009, 09:41 AM
n8kindt's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: Southern California
Posts: 221

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
  #5  
Old June 10th, 2009, 10:53 AM
Member
 
Join Date: Nov 2007
Posts: 58

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!
  #6  
Old June 10th, 2009, 11:22 AM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,858
Provided Answers: 9

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 Threads
Thread Thread Starter Forum Replies Last Post
Parse error matheussousuke answers 5 September 18th, 2009 12:55 AM
Command line PHP: suppress warnings? Franz Von Bayros answers 5 January 9th, 2008 12:35 AM
MySQL connect failure Bruce A. Julseth answers 18 March 22nd, 2007 01:05 PM