I have a php site. For all of the page links I use foo.htm, and internally rewrite this to foo.php with .htaccess:
RewriteRule ^(.*)\.htm$ $1.php [NC,L]
This works great - however, it still allows you to use the foo.php url. I would like to 301 redirect foo.php to foo.htm to prevent any old foo.php search engine results from hanging around and rewrite the foo.htm url internally开发者_StackOverflow社区 to foo.php
I can't figure out how to do this without creating a loop.
Put this code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s(.+)\.php [NC]
RewriteRule ^ %1.htm [R=301,L]
RewriteRule ^(.*)\.htm$ /$1.php [L,NC]
RewriteRule ^(.*)\.htm$ $1.php [NC,L]
RewriteRule ^(.*)\.php$ $1.htm [NC,R]
the option L stops the rewriting engine, preventing a loop.
精彩评论