htaccess将http://example.com重写为http://www.example.com,但https://www.example.com改为https://example.com

时间:2014-02-06 20:18:44

标签: .htaccess http mod-rewrite redirect https

使用.htaccess重写,我想:

http://example.com重定向到:http://www.example.com https://www.example.com重定向到:https://example.com

这是最好的方法吗?

我开始时:

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

1 个答案:

答案 0 :(得分:1)

您需要2条规则:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]