Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I bought a domain that had various subdomains such as

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

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.$
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*)$ http:/ / www.$1 [L,R=301]

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

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
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
up vote 0 down vote accepted

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

RewriteEngine On
RewriteRule ^/(.*)$$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 "", 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} !^$
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*)$$1 [L,R=301]

If the requested host is not "" and it's not blank (http 1.0 and broken clients that don't send a Host: header), then it redirects the user to 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 . [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} !^$ [NC]

RewriteRule ^(.*)$ [L,R=301]

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

If you want to keep the folder after the hostname, so that redirects to, you can replace the last line with:

RewriteRule ^(.*)$$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, so I guess they were conflicting each other. I managed to fix it but change to from the above examples. Thanks very much for your help Niall – Karim Jan 6 '11 at 1:11

Your Answer


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.