Redirecting Subdomain to WWW using .htaccess

2019-09-08 05:37发布

问题:

I'm trying to redirect a specific subdomain to www. I already have rules in place that will redirect non-www to www.domain.com. Here is what I currently have, and it works fine:

RewriteCond %{HTTP_HOST} !^www\.               [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$       http://www.%1/$1      [R=301,L]

My problem is that some requests are coming in for ns2.domain.com, and I would like those redirected to www.domain.com as well. Any help?

回答1:

Try adding an extra [^.]+\. in your second condition:

RewriteCond %{HTTP_HOST} !^www\.               [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$       http://www.%1/$1      [R=301,L]