Extensionless URL trailing slash redirect
I tested the code below on several Dreamhost domains, and it works, except on newer domains added circa 2012
RewriteEngine on
#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.*)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.*)\.php$ $1 [R=301,L]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
example that works
ryanve.com/resources/lame/ and ryanve.com/resources/lame.php both redirect to ryanve.com/resources/lame and the actual file开发者_运维技巧 that is served is lame.php
broken example
airve.com/test/file properly serves file.php but the redirects airve.com/test/file/ and airve.com/test/file.php don't work. Try them and you'll see they seem to be spitting out the internal absolute path. The same thing occurred when I tried each of the redirects independently and when I tried the basic redirect below.
Redirect 301 /test/file/ /test/file
There is nothing else in .htaccess. Any idea on what the issue is?
You are redirecting to $1
in both cases, and you don't have a RewriteBase directive to tell Apache where to root such a relative path.
Try adding a root slash before the $1 to anchor it to the root of your webserver, for instance:
RewriteRule ^(.*)$ /$1.php [L]
精彩评论