SSL 强制 URL 重写

SSL 强制 URL 重写

我正在尝试强制特定的 URL 使用 HTTPS,但问题是该页面中的所有链接也会指向其他页面的 HTTPS。

所以,我正在考虑像这样更新我的虚拟主机文件。

<VirtualHost *:80>
    #Rewrite urls under /foo/bar/* to use HTTPS://domain/foo/bar/*
    RewriteEngine on
    RewriteRule ^/foo/bar/(.*) https://domain/foo/bar/$1
</VirtualHost>

<VirtualHost *:443>
    #Rewrite all urls except /foo/bar/* to use HTTP://domain/*  
</VirtualHost>

关于如何实现这一点,有什么建议吗?

答案1

您应该将 [R,L] 标志添加到规则中。L 可能是多余的,因为它是唯一的规则,但如果您稍后添加其他规则,如果此规则处理请求,L 会阻止它运行任何其他规则。R 使其成为重定向,因此客户端实际上会向 https:// 站点发送重定向命令。

对于 :443 虚拟主机,您需要使用 RewriteCond 来排除路径为 /foo/bar/* 的情况

RewriteCond %{REQUEST_URI} !^/foo/bar/
RewriteRule ^/(.*)$ http://domain/$1 [R,L]

这基本上意味着如果 REQUEST_URI 不以 /foo/bar/ 开头,则重定向到 http://

相关内容