Forum Moderators: phranque
I have a www.domain.com and serveral subdomain.domain.com. Recently, in Google index, all of these sites are replaced with "domain.com" and "www.subdomain.domain.com".
So, I need advice on how to redirect the
www.subdomain.domain.com to subdomain.domain.com
and
domain.com to www.domain.com
I found the following code somewhere in this forum
RewriteEngine On
RewriteCond %{HTTP_HOST}!^www\.widget\.com
RewriteRule (.*) [widget.com...] [R=301,L]
but I'm afraid that it will redirect all the subdomain to the www.domain.com site.
Another question is if it possible to disable the www.subdomain.domain.com and the domain.com.
You'd undoubtedly be happier using a positive-match pattern, instead of the negative match used in that example. The code you posted redirects anything *except* www.widget.com to www.widget.com.
RewriteEngine On
#
# Redirect www.<subdomain>.example.com/<anything> to <subdomain>.example.com/<anything>
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]
#
# Redirect example.com/<anything> to www.example.com/<anything>
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Jim
[edited by: jdMorgan at 5:18 am (utc) on July 19, 2005]
[edit reason] example.com, please. [/edit]
Well, I don't agree, but you could do that. However, I'd recommend you simply reverse the two rulesets above for the sake of efficiency (process example.com requests first), and then forget about it. Those few who try to access your site by its alternate names will be redirected, find what they are looking for and be happy, and the performance impact on your server will be negligible. Similarly, search engine spiders, if they happen to find a link to one of your subdomains, will be redirected as well, and will therefore transfer the link credit to the correct domain and list it instead of the subdomain.
It's a more pleasant user experience that getting "slapped in the face" with an unexpected "403-Forbidden" or "Domain cannot be resolved" error message.
Jim