【问题标题】:Redirect www to https non www将 www 重定向到 https 非 www
【发布时间】:2016-09-09 21:31:20
【问题描述】:

我正在尝试将我的网站从 www 重定向到 https 非 www。我在 htaccess 文件中有这段代码:

RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([0-9a-zA-Z].+)$ /page.php?title=$1 [L]

ErrorDocument 404 /404.php

您能否建议对 .htaccess 文件进行更改?

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    试试这个:

    RewriteEngine On
    

    将您的网址与 www 匹配。并在没有 www 的情况下将其重写为 SSL https。

    RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
    RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
    

    匹配没有 SSL https 的 URL

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

    【讨论】: