Forum Moderators: phranque

Message Too Old, No Replies

Redirect from multiple subdomains to the main one

         

welcome3

12:54 pm on Jul 21, 2010 (gmt 0)

10+ Year Member



Hello, everybody!

I have a big trouble with my website since rewrite rules were not configurated properly. Now I have lots of pages in index of google.

the main domain is website.com

I have a rule in a .htaccess file to redirect all the users from www.website.com to website.com:

RewriteCond %{HTTP_HOST} ^www.website.com$ [NC] 
RewriteRule ^(.*)$ http://website.com/$1 [R=301,L]


It's working properly, all the query parameters are transmitting to the main domain.

But now I've got a trouble.

There a lots of pages as:

haha.blabla.website.com/page
trololo.blablabla.website.com/page2.html
bla.website.com/2008/08/29/hahaha.html


and so on...

in the search engine's index.

All of the pages listed above have their proper address as:

http://website.com/page
http://website.com/page2.html
http://website.com/2008/08/29/hahaha.html


How can I make a 301 redirect from all of the subdomains to the main domain, keeping all the query parameters?

I want all of the subdomain's to be redirected to a main one.

subdomain.website.com/
subdomain1.subdomain.website.com/
subdomain2.subdomain1.subdomain.website.com/
should send me to
website.com

keeping the query string

Please, help me. :)

phranque

1:33 pm on Jul 21, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



this should work:

RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]


as long as you have no meaningful subdomains...

welcome3

1:42 pm on Jul 21, 2010 (gmt 0)

10+ Year Member



Thanks a lot, phranque!

jdMorgan

6:52 am on Jul 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't accept mis-cased hostnames, or FQDN-formatted hostnames, or appended port numbers.
Also, don't try to redirect a blank hostname request, because you may be dealing with a poorly-coded HTTP/1.0-spoofing user-agent...

RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Jim