【问题标题】:https non www redirecthttps 非 www 重定向
【发布时间】:2016-01-26 03:25:38
【问题描述】:

我正在尝试让我的所有 URL 重定向到 https:// 而没有 www.

如果有人输入www.http://http://,他们会被重定向到https://,如果他们输入https://www.,那么问题就来了,那么它不会重定向到https://website

我在htaccess 中使用以下内容,有什么建议吗?

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

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

【问题讨论】:

  • 没有多大意义。 if (https is off and the host has www) then redirect,后跟if (https is not on, aka it's off) then redirect

标签: php .htaccess ssl https


【解决方案1】:

这条规则可以同时满足这两个要求:

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

确保删除显示的规则并在新浏览器中测试以避免旧缓存。

【讨论】:

  • 谢谢,它似乎适用于除 chrome 之外的所有东西,它仍然保留 www。
  • 这是由于 Chrome 中的旧缓存所致。清除所有缓存数据并重新测试。
  • @Andy 我敢肯定它不仅仅是 Chrome。我在其他浏览器中也遇到过类似的情况。
  • @Andy 阅读这篇文章,这对我 wiki.apache.org/httpd/RewriteHTTPToHTTPS 的类似情况有所帮助。旁注:我希望你不介意我在这里发帖 anubhava。如果你愿意,我可以把它移到 OP 的 cmets 部分。
  • @Fred-ii- 这里非常欢迎您的 cmets,并将帮助该问题的未来读者。
猜你喜欢
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 2016-04-27
  • 1970-01-01
相关资源
最近更新 更多