htaccess的重定向循环问题 [英] .htaccess redirect loop issue
问题描述
如果我去domain.com,网站重定向到domain.com/en~~V,预计。但随后的最后一条规则踢它扔了一个循环让我的网址是这个样子:
<$p$p><$c$c>http://domain.com/en/?lang=en&request=&site=basecamp&lang=en&request=&site=basecamp&lang=en&request=&site=basecamp&lang=en&request=&site=basecamp
.htaccess文件
选项+了FollowSymLinks
RewriteEngine叙述上
的RewriteBase /
的RewriteCond%{REQUEST_URI} /!(内容|图片| CSS | JS |字体| PDF文件)/
重写规则/([^.]+\.(jpe?g|gif|bmp|png|css|js|eot|svg|ttf|ico|pdf))$ / $ 1 [NC,R,L]
的RewriteCond%{REQUEST_URI}!(内容|图片| CSS | JS |字体| PDF文件)/.*
重写规则!^ [A-Z] {2} / /烯/ [NC,L,R]
的RewriteCond%{REQUEST_URI} /(INIT \ .PHP |内容|图片| CSS | JS |字体| PDF文件)!/
重写规则^([AZ] {2})1 / $的init.php LANG = $&AMP(*);?请求= $ 2及位置=大本营[L,QSA]
为什么htaccess文件重定向到/?GET_VARS代替/init.php?GET_VARS?
和我怎样才能避免这种循环?
可以通过删除的开头和结尾斜杠改变你的最后一个条件。你重写你的URI:
/init.php
但有没有尾随斜线像有在您所提供的条件,因此该规则被再次应用。它应该是这样的:
的RewriteCond%{REQUEST_URI}(INIT \ .PHP |内容|图片| CSS | JS |字体| PDF文件)!
不知道为什么你坚持要把最后的斜线在你的病情终于结束了:
#本/在这里,它永远不会匹配的init.php -------- v
的RewriteCond%{REQUEST_URI} /(INIT \ .PHP |内容|图片| CSS | JS |字体| PDF文件)!/
但你可以通过只直接排除的init.php解决这个问题:
的RewriteCond%{REQUEST_URI}!的init.php
所以,只是这样很明显,最后一组的条件/规则将是这样的:
的RewriteCond%{REQUEST_URI}!的init.php
的RewriteCond%{REQUEST_URI} /!(内容|图片| CSS | JS |字体| PDF文件)/
重写规则^([AZ] {2})1 / $的init.php LANG = $&AMP(*);?请求= $ 2及位置=大本营[L,QSA]
或者最坏的情况下,只需添加这在你的规则一开始:
的RewriteCond%{ENV:REDIRECT_STATUS} 200
重写规则^ - [L]
其中prevents任何重写引擎的完全循环
If I go to domain.com, site redirects to domain.com/en, which is expected. But then the last rule kicks in and throws it in a loop making my url look something like this:
http://domain.com/en/?lang=en&request=&site=basecamp&lang=en&request=&site=basecamp&lang=en&request=&site=basecamp&lang=en&request=&site=basecamp
.htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !/(content|images|css|js|fonts|pdfs)/
RewriteRule /([^.]+\.(jpe?g|gif|bmp|png|css|js|eot|svg|ttf|ico|pdf))$ /$1 [NC,R,L]
RewriteCond %{REQUEST_URI} !(content|images|css|js|fonts|pdfs)/.*
RewriteRule !^[a-z]{2}/ /en/ [NC,L,R]
RewriteCond %{REQUEST_URI} !/(init\.php|content|images|css|js|fonts|pdfs)/
RewriteRule ^([a-z]{2})/(.*)$ init.php?lang=$1&request=$2&site=basecamp[L,QSA]
Why is the htaccess file redirecting to /?GET_VARS instead of /init.php?GET_VARS ?
And how can I avoid this loop?
Try changing your last condition by removing the leading and trailing slashes. You've rewritten your URI to:
/init.php
But there's no trailing slash like there is in the condition that you've provided, so the rule gets applied again. It should look like:
RewriteCond %{REQUEST_URI} !(init\.php|content|images|css|js|fonts|pdfs)
Not sure why you insist on having the trailing slash at the end of your condition here:
# with this / here, it will never match init.php --------v
RewriteCond %{REQUEST_URI} !/(init\.php|content|images|css|js|fonts|pdfs)/
But you can solve it by just excluding init.php directly:
RewriteCond %{REQUEST_URI} !init.php
So, just so it's clear, your last set of conditions/rule will look like:
RewriteCond %{REQUEST_URI} !init.php
RewriteCond %{REQUEST_URI} !/(content|images|css|js|fonts|pdfs)/
RewriteRule ^([a-z]{2})/(.*)$ init.php?lang=$1&request=$2&site=basecamp [L,QSA]
Or worst case, just add this in the very beginning of your rules:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
Which prevents any kind of rewrite engine looping altogether.
这篇关于htaccess的重定向循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!