Redirecting Subdomain to WWW using .htaccess

2019-09-08 04:43发布

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条回答
Summer. ? 凉城
2楼-- · 2019-09-08 05:33

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]
查看更多
登录 后发表回答