我目前在.htaccess
文件中有两个重写规则,现在需要添加另一个。但是,GTMetrix已经在这里给我一个F评级,说明:避免登陆页面重定向
www.
添加到URL。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]
有没有办法将这些组合成一个重定向?或者,着陆页重定向不是那么糟糕吗?
您的第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]
在测试此更改之前,请务必清除浏览器缓存。