在.htaccess中循环重定向

时间:2015-09-01 20:59:15

标签: .htaccess mod-rewrite

我有这个简单的.htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} !www.mydom.net [NC]
RewriteRule (.*) www.mydom.net/$1 [R=301]

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]

正常工作,但使用查询字符串mydom.net/?view=xyz输入除外;然后进入重定向循环:

https://mydom.net/var/www/domdir/www.mydom.net/var/www/domdir/www.mydom.net/var/www/domdir/...

但更奇怪的是为什么首先插入DocumentRoot=/var/www/domdir

出了什么问题?

添加了对记录的说明:这是为了强制所有对mydom.net的请求都是https://www.mydom.net有或没有查询字符串。

1 个答案:

答案 0 :(得分:3)

Why a query string would make a difference sounds like a non-related browser cashing issue from your 301 permanent redirect. Let's say they entered http://mydom.net/?view=xyz The first rule doesn't begin with http or https so it treats your rewrite like a directory. You'd be rewritten to http://mydom.net/www.mydom.net then, because your first rule has no L flag your 2nd rule applies, so you get rewritten to: https://mydom.net/www.mydom.net the .htaccess file is read from the top since the URL changed. no www. detected, so you get rewritten to http://mydom.net/var/www/domdir/www.mydom.net/ etc.

I believe a fix would be something close to:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.mydom.net [NC]
RewriteCond %{REQUEST_URI} ^/?(.*)$
RewriteRule ^ https://www.mydom.net/%1 [R=301,L]

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/?(.*)$
RewriteRule ^ https://%{HTTP_HOST}/%1 [R,L]