您必须要么明确说明域名,要么只假设特定数量的 TLD(否则会变得更难)。
明确的域:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com [NC]
RewriteCond %{HTTPS}s::%1 ^(on(s)|offs)::(.*)$
RewriteRule ^ http%2://%3.domain.com%{REQUEST_URI} [R=301,L]
或只有 1 个顶级域名:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\.([^.]+\.[^.]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]{2,4})$ [NC]
RewriteCond %{HTTPS}s::%1 ^(on(s)|offs)::(.*)$
RewriteRule ^ http%2://%3.domain.com%{REQUEST_URI} [R=301,L]