Removing .php extension with .htaccess
| url: | http://www.webmasterworld.com/apache/3497556.htm Just remember that regex works on a character basis, and things will be clearer. The two most useful (and analogous) descriptions of "[^.]+" are, "match one or more sequential characters not a full stop" or "match one or more characters until you find a full stop." |
|---|
Also, you may need the 'other half' of this code -- a rule that maps a request for /requested-pagename back to /filename.php. On review, this may be what was missing in the original thread. So the whole solution (in addition to changing the links on your pages, which you're presumably doing in php) would be:
# Externally redirect direct client requests for .php files to non-.php URLs
rewriteCond %{THE_REQUEST} ^GET\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
rewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite extensionless page URLs to php files
# if no extension or trailing slash on requested URL
rewriteCond %{REQUEST_URI} !(\.¦/$)
# and if filename exists when .php is appended
rewriteCond %{REQUEST_FILENAME}.php -f
rewriteRule (.+) /$1.php [L]
This code is generalized to handle any number of directory levels and/or appended query strings, and I removed the rewriteConds for excluded URLs, only the second of which might be needed for your site.
