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

Say I have a folder ~/apache/ that contains two website directories:

Right now I have things set up using the sites-available and sites-enabled configurations which map domains to directories. I'd like to make it so that all I have to do is create a new directory and point the proper domain to my server.

I'll be honest and say I know little to nothing about server configurations. I think I need something along the lines of

VirtualDocumentRoot ~/apache/%0

in a configuration file. httpd.conf? apache2.conf? Or would it be best to put this in a separate config and include it in a main config?

share|improve this question
up vote 3 down vote accepted

You can potentially completed ditch name-based virtual hosting completely, turn off all your sites-enabled files, and put VirtualDocumentRoot in the main server config.

More likely, what you'll want to do is create a new vhost in sites-available and symlink it to sites-enabled normally, having it act as a catch all. Naming is significant; it needs to be the first file alphabetically in order to be treated as a default.


  • Create an alphabetically-early file that will get loaded first, to be treated as default, such as /etc/apache2/sites-available/0-dynamic.
  • Give the new vhost file some content like this:

    <VirtualHost *:80>
        ServerName catchall
        VirtualDocumentRoot /path/to/domains/%0
        <Directory /path/to/domains>
            Order Allow,Deny
            Allow from all
  • Enable it: a2ensite 0-dynamic. (if the default is still in place at /etc/apache2/sites-enabled/000-default, rename it so it comes after the new one alphabetically.

This configuration will serve up the dynamically-configured sites from the VirtualDocumentRoot, but can be overridden and served by a different <VirtualHost> when that block has a ServerName or ServerAlias that matches the request. This can be useful if your "blanket" vhost's settings are not appropriate for specific domains.

Edit: (Adding some things I experienced trying to set this up that may be helpful. All credit goes to the original author!)


If you get an error saying

Invalid command 'VirtualDocumentRoot', perhaps misspelled...

check to make sure the vhost_alias module is enabled. If it's not, run

>sudo a2enmod vhost_alias

Handling leading www

Add the following rules (be sure to replace example with your actual domain) to the 0-default file to handle and

RewriteEngine On
RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

In this example, the www is stripped for all requests, so the directory should be

share|improve this answer
Will this work for and (without the www)? And awesome info. I appreciate the thorough answer. Being able to configure specifics for certain domains is a good call. I'll be taking a look at this later tonight. – loeschg Aug 6 '12 at 14:21
@loeschg No, it does not handle the www/no-www difference. How do you want that to be handled - redirect one to the other? Or serve the content for both? If you're redirecting, that's probably best to handle with mod_rewrite to dynamically strip or add the www as desired. If you want the content to be served, then you can either handle that in mod_rewrite or turn on Options +FollowSymLinks and create a symlink for in the websites directory pointing to the directory. – Shane Madden Aug 6 '12 at 18:21
Chose to go the mod_rewrite route. Thanks again! – loeschg Aug 19 '12 at 19:49

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.