Apache Rewriterule change

Sean Gordon asked:

I want specific pages without an extension to direct to their php counterpart. Right now, I have this:

RewriteEngine on
RewriteRule ^(login|logout)$ $1.php
RewriteRule ^(login|logout)?(.*)$ $1.php?$2

Is there a way to do this in one rule? Should I do this in one rule?

My answer:


This will generalize it for just about any possible filename. Tweak the pattern match if you need to. (Thank you to the Internet for this example.)

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?([a-zA-Z0-9]+)$ $1.php [L]

Or you can enable Options +MultiViews but this may carry a performance penalty, since while it works it’s not what MultiViews was designed for.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.