mod_rewrite的错误310 TOO_MANY_REDIRECTS

人气:1,045 发布:2022-09-10 标签: .htaccess mod-rewrite

问题描述

我想使pretty的URL(contact.php ID =东西联系/东西吗?)这code在.htaccess,但是当我用它我的浏览器显示错误310 - 过多的重定向

I want make pretty URL (contact.php?id=something to contact/something) with this code in .htaccess, but when I use it my browser displays error 310 - too many redirects.

Options +FollowSymlinks

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^contact/(.*)$ contact.php?id=$1 [L]

RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^contact.php$ /contact/%1? [R,L]

有人可以帮我,有什么不好? 谢谢。

Can somebody help me, what is wrong? Thanks.

推荐答案

您的问题是,要重定向联系人/到contact.php然后重定向contact.php联系(见的无限循环?)

You problem is that you are redirecting contact/ to contact.php then redirecting contact.php to contact (see the infinite loop ?)

要解决这个问题,你可以只添加一个无用的参数,以第一条规则(另一件事是,你应该使用R = 301在过去的规则,而不是仅仅R标志,这意味着重定向是您永​​久而不是暂时的,而是这不是造成任何问题):

To fix this you can just add another useless parameter to the first rule (another thing is that you should use R=301 in the last rule instead of just R flag, this mean that the redirection is permanant and not temporary, but that's not causing any issue) :

Options +FollowSymlinks

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^contact/(.*)$ contact.php?id=$1&r=0 [L]

RewriteCond %{QUERY_STRING} ^id=([^\&]*)$
RewriteRule ^contact.php$ /contact/%1? [R=301,L]

363