Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I bought a domain that had various subdomains such as

Kim.domain.com/whatever
john.domain.com/whatever1
Lizo.domain.com/whatever2
Simon.domain.com/whatever1

And this was in the thousands, and also had links to these pages

I'd like to do a 301 redirect for all these urls into http://domain.com

Any idea how this could be done?

This is for a apache web server and needs to be done via .htaccess

I have implemented the solution from reading the answer below.

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

However I have a slight problem, I would like to redirect all subdomains + subfolders to http://www. domain.com/

With the exception of http: //domain. com/subfolder/, in which case I would like to redirect to http: // www.domain. com/subfolder/ [i.e. exception for no subdomain]

I'm guessing I need to add an exception, what can I do to implement this.

Note: example URLs above have had spaces added to them to prevent spam blocks for blocking the post.

share|improve this question
2  
Do you have any subdomains you DON'T want to rewrite? Working backwards that way would be quicker. –  Mark Henderson Jan 5 '11 at 23:28
    
Nope, I want to rewrite all the domains. The answer below helped me find the solution. Thanks –  Karim Jan 6 '11 at 1:14

2 Answers 2

up vote 0 down vote accepted

Do you intend to lose the "kim" part and retain the "whatever" part so that kim.domain.com/whatever redirects to domain.com/whatever ? If so, then drop this into every site's .htaccess file:

RewriteEngine On
RewriteRule ^/(.*)$ http://domain.com/$1 [L,R=301]

I assume you want this in .htaccess because you want to get rid of kim, john, lizo and simon but keep larry, moe and curly. If you do this, then I suggest creating one htaccess file in one directory, and setting that directory as the DocumentRoot of all of the virtualhosts you want to "get rid of".

If you want to get rid of everything but "domain.com", you can improve performance by getting rid of all the virtual hosts entirely, having just one site, and in your apache configuration doing (see "Canonical Hostnames")

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

If the requested host is not "domain.com" and it's not blank (http 1.0 and broken clients that don't send a Host: header), then it redirects the user to domain.com. This saves the server from having to figure out what virtual host a client wants then looking for and reading the htaccess file.

If you don't want the "whatever" part, RewriteRule becomes

RewriteRule . http://domain.com/ [L,R=301]
share|improve this answer
    
Thanks for taking the time to answer your the question, it was very thorugh and covered all the bases. I found the other guys answer to work, after a few bit of playing around. He got there just a second or so before you. p.p.s ignore the previous message thanks –  Karim Jan 6 '11 at 1:13

Something like:

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} !^domain.com$ [NC]

RewriteRule ^(.*)$ http://domain.com/ [L,R=301]

Basically what this says is that if the HTTP_HOST doesn't match domain.com, do a 301 redirect to http://domain.com. Be careful to escape any . in the in RewriteCond.

If you want to keep the folder after the hostname, so that http://kim.domain.com/whatever redirects to http://domain.com/whatever, you can replace the last line with:

RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

share|improve this answer
    
Thanks very much, worked perfectly.. I left a few comments before about this not working, I managed to figure it out. I have the site set up to force www.domain.com, so I guess they were conflicting each other. I managed to fix it but change domain.com to www.domain.com from the above examples. Thanks very much for your help Niall –  Karim Jan 6 '11 at 1:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.