Take the 2-minute tour ×
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 domain.com. This domain goes to a server with apache on this server I'm running three sites for that domain which are:

domain.com (default) dev.domain.com mail.domain.com

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

I want www.domain.com to redirect me to domain.com, I want development.domain.com to redirect me to dev.domain.com, I want email.domain.com and epost.domain.com to redirect me to mail.domain.com and I want all other subdomains to redirect me to the default domain.com 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 domain.com, dev.domain.com and mail.domain.com
  • then, configure virtual hosts for development.domain.com, epost.domain.com and email.domain.com
  • the last one for the redirection of www.domain.com and the other subdomains.

For instance, in order to redirect email.domain.com to mail.domain.com, you can use something similar to the following:

<VirtualHost *:80>
  ServerName email.domain.com
  ServerAlias epost.domain.com

  <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^(.*) http://mail.domain.com$1 [R=301,L]

You can adapt it for development.domain.com.

Regarding www.domain.com, you can use *.domain.com 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 domain.com 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 domain.com, dev.domain.com and mail.domain.com. I would suggest you to configure everything step by step: (1) Configure domain.com, dev.domain.com and mail.domain.com. When they are working -> (2) Configure the virtualhost for development.domain.com and check that the redirection to dev.domain.com is working, and that domain.com, mail.domain.com and dev.domain.com are still working. (3) Configure email.domain.com and check again that everything is still OK. (4) Configure www.domain.com and *.domain.com –  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.