且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

htaccess的重定向循环问题

更新时间:2023-10-21 17:12:46

可以通过删除的开头和结尾斜杠改变你的最后一个条件。你重写你的URI:

  /init.php
 

但有没有尾随斜线像有在您所提供的条件,因此该规则被再次应用。它应该是这样的:

 的RewriteCond%{REQUEST_URI}(INIT \ .PHP |内容|图片| CSS | JS |字体| P​​DF文件)!
 


不知道为什么你坚持要把最后的斜线在你的病情终于结束了:

 #本/在这里,它永远不会匹配的init.php -------- v
的RewriteCond%{REQUEST_URI} /(INIT \ .PHP |内容|图片| CSS | JS |字体| P​​DF文件)!/
 

但你可以通过只直接排除的init.php解决这个问题:

 的RewriteCond%{REQUEST_URI}!的init.php
 

所以,只是这样很明显,最后一组的条件/规则将是这样的:

 的RewriteCond%{REQUEST_URI}!的init.php
的RewriteCond%{REQUEST_URI} /!(内容|图片| CSS | JS |字体| P​​DF文件)/
重写规则^([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.