一个简单的 301 重定向在我的 htaccess 中创建了一个无限循环?
A simple 301 redirect creates an infinite loop in my htaccess?
我的网站 example.com/foo.php
上有这个 url,我通过在我的 htaccess 中这样做将其更改为 example.com/foo
:
RewriteRule ^foo$ foo.php [NC,L]
而且效果很好。但是我想做一个从旧 url 到新 url 的 301 重定向。所以我在我的 htaccess 中添加了这一行:
RewriteRule ^foo.php$ http://example.com/foo [L,NC,R=301]
我得到了一个无限循环的重定向...如何解决这个问题?
您可以使用 THE_REQUEST 变量避免循环错误:
RewriteCond %{THE_REQUEST} /foo\.php [NC]
RewriteRule ^foo\.php$ http://example.com/foo [L,R=301]
RewriteRule ^foo$ foo.php [NC,L]
或者,如果您使用的是 apache 2.4,则可以使用 END 标志
RewriteRule ^foo\.php$ http://example.com/foo [L,R=301]
RewriteRule ^foo$ foo.php [NC,END]
如果您更改顺序,它不应该循环工作。试试下面的效果:
RewriteEngine on
RewriteRule ^foo.php$ http://example.com/foo [L,NC,R=301]
RewriteRule ^foo$ foo.php [NC,L]
我的网站 example.com/foo.php
上有这个 url,我通过在我的 htaccess 中这样做将其更改为 example.com/foo
:
RewriteRule ^foo$ foo.php [NC,L]
而且效果很好。但是我想做一个从旧 url 到新 url 的 301 重定向。所以我在我的 htaccess 中添加了这一行:
RewriteRule ^foo.php$ http://example.com/foo [L,NC,R=301]
我得到了一个无限循环的重定向...如何解决这个问题?
您可以使用 THE_REQUEST 变量避免循环错误:
RewriteCond %{THE_REQUEST} /foo\.php [NC]
RewriteRule ^foo\.php$ http://example.com/foo [L,R=301]
RewriteRule ^foo$ foo.php [NC,L]
或者,如果您使用的是 apache 2.4,则可以使用 END 标志
RewriteRule ^foo\.php$ http://example.com/foo [L,R=301]
RewriteRule ^foo$ foo.php [NC,END]
如果您更改顺序,它不应该循环工作。试试下面的效果:
RewriteEngine on
RewriteRule ^foo.php$ http://example.com/foo [L,NC,R=301]
RewriteRule ^foo$ foo.php [NC,L]