Mod rewrite mod_rewrite:2个文件应始终为SSL,其余文件始终为HTTP

Mod rewrite mod_rewrite:2个文件应始终为SSL,其余文件始终为HTTP,mod-rewrite,ssl,https,Mod Rewrite,Ssl,Https,我希望确保应用程序中的少量URL始终通过SSL提供服务;其余部分应始终通过HTTP提供服务 我当前的mod_重写规则集如下所示: RewriteCond %{HTTPS} off RewriteRule ^/?account.html$ https://example.com/account.html [L] RewriteRule ^/?checkout.html$ https://example.com/checkout.html [L] RewriteCo

我希望确保应用程序中的少量URL始终通过SSL提供服务;其余部分应始终通过HTTP提供服务

我当前的mod_重写规则集如下所示:

RewriteCond %{HTTPS}            off
RewriteRule ^/?account.html$    https://example.com/account.html [L]
RewriteRule ^/?checkout.html$   https://example.com/checkout.html [L]

RewriteCond %{HTTPS}            on
RewriteCond %{REQUEST_URI}      !/account.html$
RewriteCond %{REQUEST_URI}      !/checkout.html$
RewriteRule (.*)                http://example.com/$1 [L]
每个RewriteCond中的第一个文件工作正常(本例中为account.html)。第二个文件不工作-服务器正在“以一种永远无法完成的方式重定向”

你有什么想法可以让它工作吗?在生产中,将有超过2个仅限SSL的URL,可能是7-10个


谢谢

您必须使用
R
标志重定向url

RewriteEngine On

# redirect account.html or checkout.html (if http) to https
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/(account|checkout)\.html$ [NC]
RewriteRule ^ https://%{HTTP_HOST}/%1.html [R,L]

# redirect other urls (if https) to http
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(account|checkout)\.html$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]

注意:我使用了
R
标志,默认情况下执行302重定向。当它工作时,请随意用
[R=301,L]
替换
[R,L]
(301重定向是一种永久性的重定向,由浏览器缓存)

非常感谢Justin-除了解决问题和回答我的问题之外,这比我的版本更整洁、更好看。祝你周末愉快!