【问题标题】:Force none WWW and HTTPS强制无 WWW 和 HTTPS
【发布时间】:2017-01-29 23:41:53
【问题描述】:

我有一个奇怪的问题。我的重写规则似乎正如我所期望的那样工作,但不适用于子页面。

www 到无 www 总是有效,但在某些页面上强制 https 无效。

所有规则都在主页上起作用,所以这一切都很好。

<IfModule mod_rewrite.c>
# Wordpress related rewrite.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

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

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

结果:

  1. http://example.com -> https://example.com(好)
  2. http://www.example.com -> https://example.com(好)
  3. https://example.com -> https://example.com(好)
  4. https://www.example.com -> https://example.com(好)

但是,子页面似乎无法正常工作:

  1. https://example.com/page -> https://example.com/page(好)
  2. https://www.example.com/page -> https://example.com/page(好)
  3. http://example.com/page -> http://example.com/page(坏)
  4. http://www.example.com/page -> http://example.com/page(坏)

知道如何改进此重写以在没有 www 和 https 的情况下工作,无论我身在何处?

谢谢

【问题讨论】:

    标签: .htaccess mod-rewrite https url-rewriting


    【解决方案1】:

    在您的 www and https rules. 块中,您重定向到 HTTP:

    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    

    由于您希望始终重定向到 HTTPS,因此也将其重定向到 HTTPS:

    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    

    【讨论】:

      【解决方案2】:

      用这个替换你的规则:

      RewriteEngine On
      RewriteBase /
      
      # remove www and turn on https in single rule
      RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
      RewriteCond %{HTTPS} off
      RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
      RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
      
      RewriteRule ^index\.php$ - [L]
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . index.php [L]
      

      顺序很重要,因此您必须在 WP 默认规则之前保留重定向规则。还要确保在测试此更改时清除浏览器缓存。

      【讨论】:

        猜你喜欢
        • 2016-04-05
        • 1970-01-01
        • 2018-05-04
        • 2013-05-31
        • 1970-01-01
        • 1970-01-01
        • 2015-12-05
        • 2011-09-14
        • 2018-07-17
        相关资源
        最近更新 更多