Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I've got a domain lets say it's This domain goes to a server with apache on this server I'm running three sites for that domain which are: (default)

Each of those domains got a directory in my ~/public-www/ folder with the same name.

I want to redirect me to, I want to redirect me to, I want and to redirect me to and I want all other subdomains to redirect me to the default site.

How should I config my apache2 sites in order to work like that and to do proper redirections. Someone told me that for searchengines to index and rank my sites properly the redirection have to be done with a 301 redirection or something like that.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You will first have to check that you have activated virtual hosting and ensure that the module mod_rewrite is loaded.

Then, write in your file httpd.conf or a separate file your different virtual hosts:

  • start with the virtual hosts for, and
  • then, configure virtual hosts for, and
  • the last one for the redirection of and the other subdomains.

For instance, in order to redirect to, you can use something similar to the following:

<VirtualHost *:80>

  <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^(.*)$1 [R=301,L]

You can adapt it for

Regarding, you can use * in the ServerAlias directive to match all other subdomains.

share|improve this answer
When I tried your RewriteRule my browser gave me this error. "This webpage has a redirect loop. The webpage at has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer." – Hultner Sep 14 '10 at 10:33
You have probably something wrong in the configuration of your virtualhosts for, and I would suggest you to configure everything step by step: (1) Configure, and When they are working -> (2) Configure the virtualhost for and check that the redirection to is working, and that, and are still working. (3) Configure and check again that everything is still OK. (4) Configure and * – uloBasEI Sep 14 '10 at 14:11
Got it working by adding RewriteCond %{HTTP_HOST} !^mail\.domain\.com$ [NC] – Hultner Sep 14 '10 at 14:51

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.