如何使用多个重写规则修复htaccess错误-ERR_TOO_MANY_REDIRECTS [英] how to fix htaccess error with multiple rewrite rule - ERR_TOO_MANY_REDIRECTS
问题描述
为什么此htaccess出现错误.它说很多重定向",请帮助谢谢
why this htaccess has error. it says "to many redirect" please help thanks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://us.domain.com/ae_en/$1 [R=301,L]
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^us\.domain\.com$
RewriteCond %{REQUEST_URI} !^/(ae_en|ae_ar) [NC]
RewriteRule ^(.*)/ae_en/%{REQUEST_URI} [R=301,L]
我不能尝试输入错误的代码,它总是显示错误
I cannot try the wrong code, it always appear error
推荐答案
您的.htaccess
有几个问题.我建议重新排序和编辑规则.
There are several issues with your .htaccess
. I would suggest both re-ordering and editing the rules.
我认为这将是更好的方法.我将在下面解释这些部分.
This would be a better approach, in my opinion. I'll explain the parts below.
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
# External redirect: Remove www from domain
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# External redirect: Ensure language code is set
RewriteCond %{HTTP_HOST} ^us\.domain\.com$
RewriteCond %{REQUEST_URI} !^/(ae_en|ae_ar) [NC]
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule ^(.*)$ /ae_en/$1 [R=301,L]
# Internal redirect: Pass everything non-existent to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
结构
首先,常规选项和设置应该位于顶部,并且只能出现一次,即
Structure
First, general options and settings should come at the top and only once, i.e.
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
第二,我想使外部URL正确显示,因此在这里将所有外部重定向归为一组-在上述代码块中以注释表示.
Second, I like to get the external URL to show up correctly, so I group all external redirects here - denoted with comments in the above code block.
第三次内部重定向",这意味着将(现在是正确的和最终的)外部URL映射到正确的脚本.
Third, internal "redirects" follow, meaning mapping the (now correct and final) external URL to the correct scripts.
此结构确保内部脚本仅在URL上匹配,而在下一个RewriteRule
匹配中针对.htaccess
重新运行时,URL不会更改.
This structure ensures that the internal scripts are only matched on a URL that will not change when re-run against the .htaccess
on the next RewriteRule
match.
我猜您在添加内部重定向后添加了index.php
重定向处理,这可能导致index.php
出现在浏览器中.因此,有了上述结构,我认为它不会再次出现.
I guess you added the index.php
redirection handling after adding in the internal redirect, which probably has caused index.php
to show up in the browser. So, with the above structure in place, I assume it will not show up again.
正如Mike Rockett在上面的评论中指出的那样,最后一个RewriteRule
中也存在错误.
As Mike Rockett pointed out in the comment above, there also was an error in the last RewriteRule
.
为避免使用最后一条规则重写为index.php
时出现重定向循环,您必须从语言代码重定向中明确排除index.php
,即
To avoid a redirect loop when rewriting to index.php
with the last rule, you have to explicitly exclude index.php
from language code redirection, i.e.
RewriteCond %{REQUEST_URI} !^/index.php [NC]
这篇关于如何使用多个重写规则修复htaccess错误-ERR_TOO_MANY_REDIRECTS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!