【问题标题】:Redirect all of the traffic to https://www. Currently I have two versions http://www and https://www将所有流量重定向到 https://www。目前我有两个版本 http://www 和 https://www
【发布时间】:2016-11-19 23:54:14
【问题描述】:

我有一个全新的站点,最初是 http://,然后我将站点重定向到 http://www,它的工作原理很吸引人,但是由于我安装了 SSL,我有两个版本的网站 http://www.example.com 和 @ 987654323@。 我已经在 .htaccess 中尝试了几个命令,但我仍然有两个版本,有人可以帮我解决这个问题,下面是我在 .htaccess 中使用的代码:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

【问题讨论】:

    标签: apache .htaccess redirect ssl mod-rewrite


    【解决方案1】:

    第一个 www 和 https 之后:

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

    【讨论】:

    • 在测试前清除缓存或尝试使用其他浏览器
    • 谢谢@croises。我已经清除了浏览器中的缓存,清漆并重新启动了 apache,但它仍然转到www
    • 问题不在这个htaccess中。当然在服务器配置中
    • 或者您使用的是非标准的 SSL 版本?
    • 任何建议如何更改服务器配置,https 和现在的 https 版本都可以工作。
    【解决方案2】:

    试试这个。

    RewriteEngine On
    RewriteCond %{HTTPS} off
    # First rewrite to HTTPS:
    # Don't put www. here. If it is already there it will be included, if not
    # the subsequent rule will catch it.
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # Now, rewrite any request to the wrong domain to use www.
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    【讨论】:

    • 不,因为您在没有 www 的情况下重定向到 https,然后再次使用 www = 2 x 301 重定向。如果先不带 www 重定向到 https//www,则无需在重定向到 https 之后。问题中也是一样的......
    • 糟糕,我刚刚注意到它与问题中的相同。我的坏:\
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 2019-05-23
    • 2018-09-26
    • 2015-04-06
    • 2013-01-06
    • 2014-05-08
    • 1970-01-01
    相关资源
    最近更新 更多