RewriteRule第一个规则有效,第二个规则不重定向到https

时间:2016-03-29 02:34:07

标签: apache .htaccess mod-rewrite

我正在尝试将两个http页面强制为https。当我登录页面时,它完美运行:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^login\.htm$ https://www.sample.com/dev/login.htm [R=301,NC,L]
RewriteRule ^register\.htm$ https://www.sample.com/dev/register.htm [R=301,NC,L]

但是,当我尝试添加第二页时,我收到服务器错误:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^login\.htm$ https://www.sample.com/dev/login.htm [R=301,NC,L]
Redirect ^register\.htm$ https://www.sample.com/dev/register.htm [R=301,NC,L]

显然,出了点问题。所以我把它改为第二页上的重定向而不是RewriteRule:

my arraylist cannot resolved.

网站回来了,但第二页并没有强制要求https。我已经检查了所有它应该工作,但它没有。非常感谢您的协助。

1 个答案:

答案 0 :(得分:1)

重写条件仅适用于紧随其后的规则。所以你有两个选择:

选项1:为每个规则重复条件

RewriteCond %{HTTPS} off
RewriteRule ^login.htm$ https://www.sample.com/dev/login.htm [R=301,NC,L]

RewriteCond %{HTTPS} off
RewriteRule ^register.htm$ https://www.sample.com/dev/register.htm [R=301,NC,L]

选项2:使用一个与两个URI匹配的规则

RewriteCond %{HTTPS} off
RewriteRule ^(login|register).htm$ https://www.sample.com/dev/$1.htm [R=301,NC,L]