在数千个 301 重写规则条目中强制实施 HTTPS



community. 我正在迁移一个古老的 ColdFusion 站点(认真的),我们实际上有数千个来自旧版本页面的 301 重定向,例如

index.cfm?id=1&this=that 

到更合理的同行,例如

/what-this-page-really-is

谷歌说要一一列出它们,这就是我正在做的。 但是,在所有这些重定向上,我们也希望强制执行HTTPS。 我们现在拥有的是:

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^thing.cfm$ /thing/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} id=1 [NC]
RewriteRule ^blah.cfm$ /awesome-sauce/? [R=301,L,NC]

它工作正常,但谷歌抱怨说,从技术上讲,这些旧链接有两个重定向。 一个可能的解决方案是从HTTPS重定向中排除*.cfm文件,并明确地使每个*.cfm重定向转到页面的HTTPS版本,如下所示:

RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !(.*)cfm$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^thing.cfm$ https://%{HTTP_HOST}/thing/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} id=1 [NC]
RewriteRule ^blah.cfm$ https://%{HTTP_HOST}/awesome-sauce/? [R=301,L,NC]

但是在数千行中,.htaccess文件中有很多额外的字符。 我知道这在 apache 配置中更好,但我现在没有这个选项。

我的问题:有没有办法设置一个标志,将HTTPS参数应用于所有后续的*.cfm 301重定向?

谢谢!

与其在每个 301 处理程序中强制执行https://,不如将http->https规则移到底部,并从重定向标志中删除R标志,以便 Google(或任何外部客户端)只获得一个重定向。

RewriteEngine On
RewriteRule ^thing.cfm$ /thing/? [L,NC]
RewriteCond %{QUERY_STRING} id=1 [NC]
RewriteRule ^blah.cfm$ /awesome-sauce/? [L,NC]
# all other rules here but remove R=301 flag
# finally do a http->https with rewritten URI
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,NE]

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

最新更新