仅当使用htaccess不存在子域时才添加www

时间:2017-08-24 22:15:11

标签: .htaccess

如果网址中没有子域名,我需要自动添加www

https://example.com => https://www.example.com
http://test.example.com => http://test.example.com (no change)

使它适用于http和https也很好。

这是我到目前为止所拥有的

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

1 个答案:

答案 0 :(得分:1)

您可以使用:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.[^.]+)$
RewriteRule ^ https://www.%2%{REQUEST_URI} [NE,L,R]
相关问题