【问题标题】:Redirect to non www is not working for pages in https重定向到非 www 不适用于 https 中的页面
【发布时间】:2017-07-12 06:12:19
【问题描述】:

我正在尝试使用 .htaccess 为此类目标添加重定向规则:

  1. 将所有 http 页面重定向到 https。
  2. 将所有 www http 和 https 页面重定向到非 www https。

我的 .htaccess 代码:

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

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

一切正常,除了一个:

  • http 重定向到 https(所有页面)
  • https www 重定向到 https 非 www(主页和子文件夹)

https://www.example.com/1/page.html 不会重定向到https://example.com/1/page.html(两个页面都打开)

有什么问题?如何编写 .htaccess 规则将所有页面重定向到 https 非 www?

【问题讨论】:

    标签: .htaccess redirect https rules no-www


    【解决方案1】:

    您可以使用它来删除www 并强制https

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

    确保在测试之前清除缓存。结合这两条规则也将有助于加快您的网站速度,而不必运行两条单独的规则。

    【讨论】:

      猜你喜欢
      • 2017-09-07
      • 1970-01-01
      • 2018-05-24
      • 2012-08-06
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      相关资源
      最近更新 更多