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

So I use following schema to handle multiple sites on my apache:


# get the server name from the Host: header
UseCanonicalName Off

VirtualDocumentRoot /var/www/%0/docs
VirtualScriptAlias /var/www/%0/cgi-bin


Therefore, if a client go to, it will actually point to /var/www/, which is good. However, what if the client go to It will point to /var/www/, which is not what we want.

So my question is: is there any better schema for that? Or what should I do to fix the issue?

share|improve this question
up vote 2 down vote accepted

This depends what behavior you want to see.

If you want for the site to be redirected to or vice-versa, this can be implemented in RewriteRule settings. For example (these go inside your <VirtualHost>):

# Send to
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^/(.*)$$1 [R=301,L]
# Send to
RewriteCond %{HTTP_HOST} ^www\.other\.example\.com$ [NC]
RewriteRule ^/(.*)$$1 [R=301,L]

To knock the www off of every name:

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

If you want to serve the content to both names, then you have a couple options.

  • If you'll only ever have a pretty simple set of domains, then change the constructed docroot to not care about www.

    VirtualDocumentRoot /var/www/%-2/docs

    ...which points to the docroot at /var/www/example/docs.

  • If you need to get a little more creative with the subdomains (like having a and that point to their own site, not the one), symlinks might be a good approach.

    Options +FollowSymLinks

    ...and create a symlink at /var/www/ pointing to the directory at /var/www/

share|improve this answer
Could you please advise how to use RewriteRule you mentioned above? An example will be much helpful, thanks! – cache Jun 27 '12 at 22:27
@cache Added an example for the redirect option. – Shane Madden Jun 27 '12 at 22:30
Thanks, what should I change to make it generic rather than only applied for – cache Jun 27 '12 at 22:48
@cache Edited that in too. – Shane Madden Jun 27 '12 at 23:04

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.