【问题标题】:Redirect www and http to non-www with https using .htaccess使用 .htaccess 使用 https 将 www 和 http 重定向到非 www
【发布时间】:2018-06-16 03:59:28
【问题描述】:

我已将以下内容写入我的 .htaccess 文件中,该文件位于我的 index.html 所在的目录中。

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

I want that 
www.example.com       
http://example.com    
and http://www.example.com      

are redirected to 
https://example.com

我必须与 example.com 交换 REQUEST_URI 还是我做错了什么?

【问题讨论】:

    标签: apache .htaccess redirect web https


    【解决方案1】:

    你必须在你的重写条件之间加上一个 OR

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

    注意:您必须将网址测试为:

    http://www.example.com

    http://example.com

    https://www.example.com

    他们都会被重定向到

    https://example.com
    

    【讨论】:

    • 哪个网址被重定向错误? www.example.com?
    • 所以把 http:// 放在开头,没有协议就没有任何请求
    • @WoffeLL 您必须将 http:// 放在要测试的每个 url 的开头,因为您服务器上所有请求的 url 肯定都会有。您可以在任何平台上提出没有任何协议的请求吗?这是一个网络标准..
    猜你喜欢
    • 2015-02-16
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2016-08-31
    相关资源
    最近更新 更多