我有一个主域,几个附加域,最近去了HTTPS。 这对我来说可行,但我希望你的意见:
## All http requests to the relative https url RewriteCond %{SERVER_PORT} 80 [OR] ## redirect the other domains and the non www main main domain RewriteCond %{HTTP_HOST} ^domain3.com [OR] RewriteCond %{HTTP_HOST} ^www.domain3.com [OR] RewriteCond %{HTTP_HOST} ^domain2.com [OR] RewriteCond %{HTTP_HOST} ^www.domain2.com [OR] RewriteCond %{HTTP_HOST} ^maindomain.com ## this stuff is for SSL RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ ## i send them all to the https main domain RewriteRule ^(.*)$ https://www.maindomain.com/$1 [L,R=301]
这是一个更好的方法来为每个条件重写规则?
当然不需要多个RewriteRule指令。 这只会使事情复杂化,效率下降。 如果您有多个规则(针对每个条件),那么即使您已经在规范协议+主机中,每个规则也至less会被处理。
但是,您似乎正在redirect每个其他域。 一切都不是www.maindomain.com 。 如果是这种情况,那么你可以简化这个规则,并消除多个条件 。 例如:
## All http requests to the relative https url RewriteCond %{SERVER_PORT} 80 [OR] ## redirect all the other non-canonical domains RewriteCond %{HTTP_HOST} !^www\.maindomain\.com ## this stuff is for SSL RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ ## i send them all to the https main domain RewriteRule (.*) https://www.maindomain.com/$1 [L,R=301]
所以,现在说…如果HTTP或不www.maindomain.com然后redirect到HTTPS和www.maindomain.com 。 而不是专门检查每个非规范的主机,你只是检查,看看它不是规范的主机。
无论如何,如果您要捕获整个URLpath,则不需要开始和结束锚点。 即。 (.*)与^(.*)$ 。
或者,你可以避免正则expression式捕获,而只是使用REQUEST_URI服务器variables(主要是以斜杠开始)。 例如:
RewriteRule ^ https://www.maindomain.com%{REQUEST_URI} [L,R=301]
## this stuff is for SSL RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
这看起来像cPanel(更新SSL证书时)在每个 RewriteRule之前自动注入的carb。 如果你在.htaccess有很多的RewriteRule指令,这会造成一个令人难以置信的臃肿的.htaccess文件。 (它也可能暴露以前不明显的漏洞)。我不知道为什么它们不在文件开始时将这些条件分解成单个块,而是否定它们(即反转)它们? 例如:
# START: this stuff is for SSL RewriteCond %{REQUEST_URI} ^/[0-9]+\..+\.cpaneldcv$ [OR] RewriteCond %{REQUEST_URI} ^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ RewriteRule ^ - [L] # END: this stuff is for SSL ## All http requests to the relative https url RewriteCond %{SERVER_PORT} 80 [OR] ## redirect all the other non-canonical domains RewriteCond %{HTTP_HOST} !^www\.maindomain\.com ## i send them all to the https main domain RewriteRule (.*) https://www.maindomain.com/$1 [L,R=301]