开发者

.htaccess: rewrite .htm urls internally to .php, but also redirect .php urls to .htm

开发者 https://www.devze.com 2023-03-09 21:28 出处:网络
I have a php site. For all of the page links I use foo.htm, and internally rewrite this to foo.php with .htaccess:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号