Created
November 7, 2008 14:48
-
-
Save mudge/22877 to your computer and use it in GitHub Desktop.
Remove file extensions in URLs with mod_rewrite but preserve 404 errors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The following will allow you to use URLs such as the following: | |
# | |
# example.com/anything | |
# example.com/anything/ | |
# | |
# Which will actually serve files such as the following: | |
# | |
# example.com/anything.html | |
# example.com/anything.php | |
# | |
# But *only if they exist*, otherwise it will report the usual 404 error. | |
Options +FollowSymLinks | |
RewriteEngine On | |
# Remove trailing slashes. | |
# e.g. example.com/foo/ will redirect to example.com/foo | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA] | |
# Redirect to HTML if it exists. | |
# e.g. example.com/foo will display the contents of example.com/foo.html | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteCond %{REQUEST_FILENAME}.html -f | |
RewriteRule ^(.+)$ $1.html [L,QSA] | |
# Redirect to PHP if it exists. | |
# e.g. example.com/foo will display the contents of example.com/foo.php | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteCond %{REQUEST_FILENAME}.php -f | |
RewriteRule ^(.+)$ $1.php [L,QSA] |
I found CharlesHenry's comment helpful, but it only worked it I changed the placement of the directory name.
From:
RewriteRule ^subdirectoryname/(.+)/$ /$1 [R=permanent,QSA]
To:
RewriteRule ^(.+)/$ /subdirectoryname/$1 [R=permanent,QSA]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey guys, if you're running your website from a subdirectory, you may need to change line 19:
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
to
RewriteRule ^subdirectoryname/(.+)/$ /$1 [R=permanent,QSA]
where 'subdirectoryname' is the folder name.
Hope this helps.