zr

mod_rewrite .htaccess script

Apr 01, 2010 00:37


Suppose you have a website made up of a bunch of PHP files:
yoursite.com/page.php
PHP extension is, of course, showing. Thats not good on many levels, which I'm not going to discuss, the question is, what can be done about that?
This tip will work if your server supports mod_rewrite.
First, lets allow incoming URLs to leave out .php. Such that, for example, the URL above can be accessed as:
yoursite.com/page
Second, lets incoming URLs with .php extension redirect to a URL sans that extension.
Both of these goals achieved with the following .htaccess script:
RewriteEngine on # internal redirect # site.com/page → site.com/page.php RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+)$ $1.php?DontStripPHPExtension [QSA,L] # external (301 Moved Permanently) redirect # site.com/page.php → site.com/page RewriteCond %{REQUEST_METHOD} ^GET$ RewriteCond %{QUERY_STRING} !^DontStripPHPExtension RewriteRule ^(.+)(\.php)$ /$1 [QSA,R=301]

apache, .htaccess, code, mod_rewrite

Previous post Next post
Up