mod_rewrite-某些页面的HTTP到HTTPS保留预先存在的重写规则

时间:2018-08-27 22:16:07

标签: apache .htaccess

我有一个.htaccess文件,其中包含一些到目前为止运行良好的规则。现在,我在网站中实现了SSL,我需要添加一条规则以将单个网页重定向到HTTPS,其余的保持不变。不幸的是,但我无法这样做。

Pre SSL代码

RewriteEngine On
RewriteBase /

#Redirect http://example.com to http://www.example.com
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]

#Rewrite http://www.example.com/123 to http://www.example.com/data.php?id=123   
RewriteRule ^([0-9]+)$  data.php?id=$1 [L,NC]

#Rewrite http://www.example.com/about to http://www.example.com/about.php
RewriteRule ^about$ about.php [L,NC]

#Rewrite http://www.example.com/ab123 to http://www.example.com/abdisp.php?id=123
RewriteRule ^ab([0-9]+)$  abdisp.php?id=$1 [L,NC]

现在,我只需要将对单个特定脚本的请求重定向到HTTPS。之前的SSL规则需要保持目前的状态,因此,我要做的就是添加以下代码:

新的HTTP到HTTPS

#If HTTPS is not set and the script being requested is rvservice.php, redirect to HTTPS
RewriteCond %{HTTPS} != on
RewriteCond %{REQUEST_URI} rvservice\.php
RewriteRule ^/rvservice\.php(.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

这不起作用。当我将此原始代码的附加内容上传到服务器时,出现内部服务器错误。我尝试将这个新代码放在原始.htaccess文件的开头,但是结果是相同的。我将其重写为单个重写规则(无条件),但是出现了重定向循环错误。我一直在阅读有关HTTP到HTTPS的SO文章,并应用了他们的解决方案,但似乎我的旧代码中有些内容与新行并不一致。我觉得我已经接近解决方案了,但是就目前而言,我似乎已经越来越无知了。我会感激任何正确方向的指点。谢谢!

1 个答案:

答案 0 :(得分:1)

在.htaccess中,在测试RewriteRule参数的开头没有/:

RewriteCond %{HTTPS} off
RewriteRule ^rvservice\.php(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

但是,如果您使用%{REQUEST_URI},则它们始终以/

开头