Forum Moderators: phranque
RewriteCond %{HTTP_HOST} ^.+www\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Given a properly-functioning search engine spider, a 301 is not dangerous. A 301 is telling the spider that the requested resource has been moved permanently. One interpretation of that would be, "We're redirecting this URL because otherwise you, Mr. spider, would see duplicate content.
Since you apparaently have wild-card DNS enabled, which will resolve any subdomain to your main domain's pages, it would be far more dangerous to not redirect spurious or non-unique subdomains.
To fix this problem, you can use either the code you posted, or the "classic" version, which redirects any non-standard domain request to the main domain:
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com
RewriteRule (.*) http://www.maindomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^w [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^.+www\.example\.com [NC]
RewriteRule (.*) http://www.example/$1 [R=301,L]
<added>
Testing proves otherwise. I need to keep them seperate:
RewriteCond %{HTTP_HOST} ^.+www\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^w [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]