【问题标题】:.htaccess rewrite consolidation.htaccess 重写合并
【发布时间】:2017-04-12 12:13:14
【问题描述】:

我的.htaccess 文件中目前有两个重写规则,现在需要添加另一个。然而,GTMetrix 已经在这里给了我一个 F 评级:避免登陆页面重定向

  1. 第一个重定向将www. 添加到 URL。

  2. 第二个重定向将一个子目录添加到 URL。

  3. 第三个(建议的)重定向将https 添加到 URL 的开头。

RewriteEngine on

#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Then, rewrite to /catalog
RewriteCond %{REQUEST_URI} !^/catalog [NC]
RewriteRule ^(.*)$ /catalog/$1 [L]

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

有没有办法将它们组合成一个重定向?或者,着陆页重定向不是那么糟糕吗?

【问题讨论】:

    标签: .htaccess redirect mod-rewrite


    【解决方案1】:

    您的第 1 条和第 3 条重定向规则可以合二为一,应保留在重写规则之前:

    RewriteEngine On
    
    # add www and turn on https in same rule
    RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
    
    #Then, rewrite to /catalog
    RewriteCond %{REQUEST_URI} !^/catalog/ [NC]
    RewriteRule ^(.*)$ /catalog/$1 [L]
    

    确保在测试此更改之前清除浏览器缓存。

    【讨论】:

    • 感谢您的快速回复!当我有机会对此进行测试时,我会通知您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    相关资源
    最近更新 更多