Remove .php file extension with .htaccess redirect

I guess it can be done, but how?

Basicly, I want to redirect /foo.php, /x/bar.php, /x/y/baz.php and /x/y/z/whatever.php to /foo, /x/bar, /x/y/baz and /x/y/z/whatever, respectively.

Since I actually have slightly more than four pages I want to redirect I don’t want to hard code them all…

Any suggestions?

Normally php scripts are not the only thing that is served by your web server. So when your trying to solve one problem you need to make sure you don’t cause more problems in the process.

So to answer just the question asked you can try something along these lines:

 AliasMatch ^(.*) $1.php

This will grab all urls and add .php to the end. And of course it will cause you countless problems with graphics, style sheets, or any static html. But if php scripts is all that you serve you got it made.

Hmm… not quite what I wanted. And it actually created an Internal Server Error.

I want to redirect /foo.php to /foo.

If you have content negotiation enabled on your server, this should already work without extra configuration.

If not, try this in your .htaccess file:

<FilesMatch "^[^\\.]+$">
ForceType application/x-httpd-php


Vinnie’s got what looks like a pretty good solution but you can also do that with mod_rewrite (on an Apache server – the setup should just take removal of the default comment - # - at the beginning of the Load Module mod_rewrite … line within httpd.conf) with the following in your root level .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_URI} !\\.php$
RewriteRule ^(.*)[color=red]+[/color]$ $1.php [L]

The RewriteCond(ition) is required to eliminate the loop which would add endless .php extensions and will rewrite EVERYTHING with a .php extension.

Since this WILL cause problems if you address a directory (including root) like this, I’ve added the + to require SOMETHING in the {REQUEST_URI} string 'cause a redirect to won’t make any sense – you’ll have to be sure that you don’t do that with subdirectories.



You don’t seem to understand what I want.

Content negotiation already works. I already removed the file extensions from the links and it works. But, links from other sites still have the php extension and Opera for some reason reloads the document adding the file extension when you click on a fragment link (e.g. #menu).

I want to do this:

Redirect /foo.php

but for all requests that ends with “.php”


Wow! That IS different!

To handle the INCOMING links to get rid of the .php (so content negotiation can replace it), we can change what I’d given you before with:

RewriteEngine On
RewriteRule ^(.*)+\\.php$ $1 [R=301,L]

That will redirect all requests for .php scripts to the same request without the .php extension AND show the new URL in the location box (sans “.php”). It leaves it to Content Negotiation to process the filename request as a php script.

NOTE: This should also leave your image links alone as they’d NOT have the .php extension.




Thanks for your reply, but it doesn’t work. I actually tried it myself before you posted. But it will make

look for

which doesn’t exist and is not what I intended.

And btw, my nickname is zcorpan, not z-corp.


Aw, I need to have a little fun with all these aliases :agree:

Anyway, there MUST be something else in your .htaccess which redirects 'cause

RewriteEngine On
RewriteRule ^(.*)+\\.php$ $1 [R=301,L]

uses the dreaded EVERYTHING atom followed by .php to redirect to EVERYTHING without the .php.

Hmmm, maybe you should omit the + as it merely requires one or more instances of the EVERYTHING atom. If that’s the cause of your problem, please accept my apologies for leaving that in from my first post (which was meant to eliminate the “directory only” request at root).