Mod_rewrite:有两个重写规则的奇怪行为


Mod_rewrite : Strange behavior with two rewrite rules

我配置了以下规则:

 <IfModule mod_rewrite.c>
                Options -MultiViews
                RewriteEngine On
                RewriteCond %{REQUEST_URI} !^/foo/bar.php
                RewriteCond %{HTTPS} !=on
                RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
                RewriteCond %{HTTP_HOST} ^(domain.com|www.domain.com)
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteRule ^(.*)$ web/app.php [QSA,L]
            </IfModule>

我希望所有使用HTTP的请求都重定向到HTTPS,除了一个特定的路径:/foo/bar.php.

第二条规则将所有内容重定向到web/app.php脚本(Symfony2)。

现在一切正常,除了当我查询http://domain.com/foo/bar.php,我被重定向到https://domain/com/web/app.php.

如果我查询https://domain/com/foo/bar.php,运行良好。

这对我来说毫无意义,为什么我会被重定向到"https://domain/com/web/app.php"?我遗漏了什么吗?

谢谢!

在第一条规则中使用THE_REQUEST变量而不是REQUEST_URITHE_REQUEST变量表示Apache从浏览器收到的原始请求,并且在执行某些重写规则后不会被覆盖。

Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} !/foo/bar.php
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^(www'.)?domain'.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ web/app.php [QSA,L]