# For sites running on a port other than 80
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R]
# And for a site running on port 80
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R]
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
3 回答
这是一个适用于任何域名的通用解决方案:
见http://www.htaccessredirected.com/force-redirecting-non-www-to-www-htaccess.htm
如果要仅将
domain.com
重定向到www.domain.com
并保留子域(例如touch.domain.com
),则必须是特定的: