【问题标题】:www and non-www http requests to https://www with full pathwww 和非 www http 请求到 https://www 的完整路径
【发布时间】:2017-06-11 09:19:21
【问题描述】:

我创建了从非 www 和 www http 请求到 https 和 www 请求的重定向,这很容易。我在 .htaccess 中使用了以下规则

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

但现在的问题是,当有人在末尾输入旧的非安全 URL(如域和 project-about.html)时,.htaccess 重定向到 https,但在末尾再次添加请求,因此域后的请求如下所示:

/project-about.htmlproject-about.html

我该如何解决?谢谢

【问题讨论】:

    标签: apache .htaccess redirect https


    【解决方案1】:

    使用这个:

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

    此方法也无需额外的RewriteRule,有助于加快您的网站速度。

    确保在测试之前清除缓存。

    【讨论】:

    • @Igor 这不会在请求中添加额外的 www。清除缓存。
    • 正如@starkeen 所说,以及我在回答中的内容,请清除您的缓存。
    【解决方案2】:

    改成这样:

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

    【讨论】:

    • 即使没有额外的重定向也能很好地工作。测试并确认。谢谢
    猜你喜欢
    • 2015-04-20
    • 1970-01-01
    • 2014-12-16
    • 2017-12-04
    • 2013-04-22
    • 2018-09-26
    • 1970-01-01
    • 2023-03-16
    • 2017-12-28
    相关资源
    最近更新 更多