在.htaccess重定向中定位HTTPS

时间:2012-01-11 23:19:22

标签: .htaccess

我目前有:

RewriteCond %{HTTP_HOST} ^www.domain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.co.uk [NC,OR]
RewriteCond %{SERVER_PORT} 80 [NC]
RewriteRule (.*) https://domain.com/$1 [R=301,L]

用于重定向www。并将域名停放到正确的最终https网址。

失败的地方是我输入https://www.domain.com

的时候

我在第一行定位非安全网址,如何在https出现时添加对重定向www的支持。

此外,当我添加子域dev.domain.com时,它会重定向到domain.com/dev我假设因为上面的.htaccess?

谢谢

1 个答案:

答案 0 :(得分:1)

将规则拆分为2个规则集。

#redirect all domains other than example.com to httpS://example.com
RewriteCond %{HTTP_HOST} !^example.com
RewriteRule (.*) https://example.com/$1 [R=301,L]

#redirect none-secure protocol to https
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://domain.com/$1 [R=301,L]

子域名不应根据您提供的规则重定向。