【问题标题】:best htaccess multi-rewrite rule最佳 htaccess 多重写规则
【发布时间】:2016-10-21 10:25:58
【问题描述】:

我需要为我的网站找到最佳的重写规则...

我有 2 个域 example.it 和 example.com,但我只有 example.com 下的 SSL。

我需要的是:每个用户请求都应该被重定向到 https://www.example.com/...

现在我正在使用这个规则:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?example\.it$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

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

但如果我输入http://example.com,它会将我重定向到https://example.com,而不是www。

另外,如果我进行 GTMetrix 测试,我会在避免登录页面重定向下获得 F 值:

Avoid landing page redirects for the following chain of redirected URLs.

http://example.it/
http://www.example.com/
https://www.example.com/

我该如何解决?谢谢。

【问题讨论】:

    标签: php apache .htaccess redirect mod-rewrite


    【解决方案1】:

    您可以在单个重定向规则中完成所有这些操作,并避免多次重定向。让您的 .htaccess 仅包含此规则:

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

    确保在测试时清除浏览器缓存。

    【讨论】:

      猜你喜欢
      • 2015-02-15
      • 2017-11-21
      • 2011-02-04
      • 2021-02-09
      • 2013-06-29
      • 1970-01-01
      • 2013-12-26
      • 2011-11-03
      相关资源
      最近更新 更多