Skip to content

Instantly share code, notes, and snippets.

@mrclay
Created March 7, 2012 15:01
Show Gist options
  • Save mrclay/1993662 to your computer and use it in GitHub Desktop.
Save mrclay/1993662 to your computer and use it in GitHub Desktop.
Remove PHP extension from URLs
<IfModule mod_rewrite.c>
RewriteEngine on
# Redirect /index.php (and /index) to /
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?index(\.php)?(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [L,R=301]
# Redirect /foo.php to /foo
RewriteCond %{THE_REQUEST} ^GET\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [L,R=301]
# Redirect /foo/ to /foo, or /foo/bar/ to /foo/bar
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule .* %1 [L,R=301]
# Make /foo and /foo/bar execute /foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME}.php (.*)
RewriteRule .* %1 [L]
# Note: If /foo.php exists, and /foo/bar/ is requested, the user will be redirected
# to /foo/bar and /foo.php will be executed. If this is unacceptable, replace
# the previous two rule blocks with these:
#
# RewriteCond %{DOCUMENT_ROOT}/current/path/$1.php -f
# RewriteRule (.*)/$ $1.php [L,R=301]
#
# RewriteCond %{DOCUMENT_ROOT}/current/path/$0.php -f
# RewriteRule .* $0.php [L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment