.htaccess - 修改当前重写规则以强制使用 SSL

标签 .htaccess ssl url-rewriting

我希望我网站上的所有页面都强制使用 SSL 连接。目前我已经准备好了......

#RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^(www)\.domain\.com$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

以上规则来源于网络,本人不是很懂。我尝试将最后一行更改为 RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L](注意 https 部分) .

当我使用 http://www.domain.com/path-to-here 测试重定向时我被重定向到 https://www.domain.com/ .

简而言之,最终目标是在保留所有原始路径的同时强制使用 www 和 https。

例如:

最佳答案

您可以使用:

RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off 
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [NE,R=301,L]

关于.htaccess - 修改当前重写规则以强制使用 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29752792/

相关文章:

Javascript 应用程序和 SEO

apache - 如何让 .htaccess 做我想做的事? :) (appending query string to url)

facebook - 无法在本地主机中开始使用 Google App Engine(runwithfriends 示例)

apache - DirectAdmin Apache - .htaccess 重写

regex - .htaccess 中的 CodeIgniter 查询字符串重写

.htaccess - 仅在 yii 网站的一个页面上强制使用 https

postgresql - SonarQube:如何连接到支持 SSL 的 Google Cloud Postgresql 服务器

c# - SslStream 上的 .NET XmlReader - 从流中读取多个 XML - 获取 XmlDocument

linux - Nginx重写规则动态子文件夹和路径

apache - URL重写和域名: how to make it single slash?后的双斜杠