【问题标题】:Redirection With https from www to non-www使用 https 从 www 重定向到非 www
【发布时间】:2016-05-24 19:25:56
【问题描述】:

我的需求很简单,我想要一套可以做以下事情的 RewriteRule

  1. 将所有 www 请求重定向到非 www。
  2. 将所有请求重定向到 https。

最终我的 URL 要求是 http://domain.com 我可以做大部分事情,但像 https://www.domain.com 这样的请求没有重定向。

这是我的重定向规则:

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

请建议我需要做什么。

【问题讨论】:

    标签: url-redirection


    【解决方案1】:

    使用下面的代码

    RewriteEngine On
    #Redirect HTTP with www to HTTPS without www
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
    
    # Redirect HTTP without www to HTTPS without www
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    # Redirect HTTPS with www to HTTPS without www
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
    

    【讨论】: