使用 HTTPS 的 ModRewrite

发布于 2024-09-18 18:54:28 字数 1459 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗化ㄋ丶相逢 2024-09-25 18:54:28

此规则:

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

...只会将请求重写为 https://rto12.ca/REQUEST_URI,然后将其传递给下一条规则(下一条规则的输入,您将其附加到请求的结尾将是 https://rto12.ca/REQUEST_URI)。然而,为了让它正常工作,你需要它立即重定向:

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

很可能将所有规则组合成最多一个重定向,所以让我稍微尝试一下,看看我能想到什么与,然后我会更新答案。不过,添加标志应该可以解决您的问题。

编辑:我认为这应该一次性完成所有事情:

RewriteEngine On

RewriteCond %{HTTPS}        =off   [OR]
RewriteCond %{HTTP_HOST}   !^www\. [OR]
RewriteCond %{THE_REQUEST}  ^[A-Z]{3,9}\ /index\.(html|php)
RewriteCond %{HTTP_HOST}    ^(www\.)?(.+)$
RewriteRule ^(index\.(html|php))|(.*)$ https://www.%2/$3 [R=301,L]

This rule:

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

...will just rewrite the request to https://rto12.ca/REQUEST_URI, and then pass it off to the next rule (the input to the next rule, which you append to the end of the request, will be https://rto12.ca/REQUEST_URI). However, for it to work properly, you need it to redirect immediately:

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

It's likely possible to combine all of your rules into at most a single redirect, so let me play around with it a bit and I'll see what I can come up with, then I'll update the answer. Adding the flags should fix your problem either way, though.

Edit: I think this should take everything in one go:

RewriteEngine On

RewriteCond %{HTTPS}        =off   [OR]
RewriteCond %{HTTP_HOST}   !^www\. [OR]
RewriteCond %{THE_REQUEST}  ^[A-Z]{3,9}\ /index\.(html|php)
RewriteCond %{HTTP_HOST}    ^(www\.)?(.+)$
RewriteRule ^(index\.(html|php))|(.*)$ https://www.%2/$3 [R=301,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文