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

.htaccess 仅当使用htaccess不存在子域时添加www,.htaccess,.htaccess,如果url中没有子域,我需要自动添加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_HO

如果url中没有子域,我需要自动添加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]
您可以使用以下选项:

RewriteEngine on

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

对于每一种可能的组合,这是否总是重写为https?