htaccess的重定向循环时,重定向和重写规则一起 [英] .htaccess redirect loop when Redirect and RewriteRule together
问题描述
我尝试以下在的.htaccess文件:
I'm attempting the following in an HTACCESS file:
我要301重定向这一点 - > http://www.domain.com /somepage.php?page=foo
I want to 301 redirect this --> http://www.domain.com/somepage.php?page=foo
本 - > http://www.domain.com/my - pretty的-URL /
这工作得很好,当我改变对网站内部链接阅读我多么希望,我不使用R = 301标志:
This works fine when I alter internal links on the site to read how I want and I DO NOT use the R=301 flag:
RewriteRule ^my-pretty-url/$ /index\.php?page=foo [L]
但是...这里顺利的我也想301重定向任何外部请求到服务器,当我处理它,它使我在重定向循环。
BUT... the hitch here is I also want to 301 Redirect any external requests to the server, which when I handle that it puts me in a redirect loop.
RewriteCond %{REQUEST_URI} /index.php$
RewriteCond %{QUERY_STRING} ^page=foo$
RewriteRule ^.*$ http://www.domain.com/my-pretty-url/? [R=301,L]
RewriteRule ^my-pretty-url/$ /index\.php?page=foo [L]
该规则的RewriteCond本身不工作,只在底部的单一重写规则的工作原理是本身为内部重写,但它不处理以外的请求。
The RewriteCond rules don't work by themselves, only the single RewriteRule at the bottom works by itself for internal rewrites, but it doesn't handle outside requests.
显然,如果我一起兼得,这是创造一个循环。我该如何解决这个问题?
Obviously, if I have both together, it's creating a loop. How do I get around this??
谢谢!
推荐答案
请尝试以下方法prevent的循环
Try the following to prevent the looping
#prevent internal redirects, and prevent loop
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} /index.php$
RewriteCond %{QUERY_STRING} ^page=foo$
RewriteRule ^.*$ http://www.domain.com/my-pretty-url/? [R=301,L]
RewriteRule ^my-pretty-url/$ /index\.php?page=foo [L]
这篇关于htaccess的重定向循环时,重定向和重写规则一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!