Create dynamic virtual hosts in Apache HTTP with vhost_alias

Apache HTTP's mod_user makes /home/user/public_html/ directories web-accessible as While useful, it is often not enough. What if you need to access /home/user/public_html/project? Sure, it is available at, but what if you prefer to access it on it's own sub-domain (such as You can create a separate Apache VirtualHost for each project or use mod_rewrite, but you'll still need a RewriteBase (for Drupal) and to confront the regex. Two options, but they both create room for errors and are yet-another development hassle.

Instead, forget about mod_user and the .htaccess file. Use mod_vhost_alias. Apache HTTP Server docs describe mod_vhost_alias thusly: This module creates dynamically configured virtual hosts, by allowing the IP address and/or the Host: header of the HTTP request to be used as part of the pathname to determine what files to serve. Exactly what you need.

(Note: this is written for Debian/Ubuntu, but can be adapted for CentOS/RHEL)
Enable mod_vhost_alias: sudo a2enmod vhost_alias

Create a new virtualhost file in your Apache HTTP settings directory (/etc/apache/sites-available for Debian/Ubuntu) named

<VirtualHost SERVER_IP_ADDRESS:80>
        ServerAdmin admin@
        ServerAlias *

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        <Directory /home/*/public_html>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all

        #Apache Environment variables are used in recent installs, set
        #log locations specifically if not available in your distribution.
        ErrorLog ${APACHE_LOG_DIR}/dev-error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/dev-access.log combined

        #URL structure
        UseCanonicalName Off
        VirtualDocumentRoot /home/%-4/public_html/%-5/

Change SERVER_IP_ADDRESS to your server's IP address. Save the file, then enable the new Virtual Host: sudo a2ensite, then reload the Apache config /etc/init.d/apache2 reload. Try out all of your new virtual hosts now. Check the dev-error.log to diagnose any 404s.

The most important part of this is the VirtualDocumentRoot setting. The percent-number values specify which part of the requested host name to use as the DocumentRoot. If values are positive the count begins on the left, if values are negative the count begins on the right. When a value isn't supplied it is replaced with an underscore.

Note: %-4 and %-5 are used instead of %2 and %1, so requests for are directed to /home/_/public_html/_, and are directed to /home/user/public_html/_. If %2 and %1 are used, requests for will be directed to /home/example/public_html/_, and will be directed to /home/dev/public_html/example; not as useful, IMO, so I use the former. Additionally, %-4 and %-5 allow point to the correct directory, so www.* and Domain Access will work.


I get failure to reload apache:
$ sudo a2ensite
Enabling site
To activate the new configuration, you need to run:
service apache2 reload
$ sudo service apache2 reload
* Reloading web server
config [fail]

When I disable the site and reload, there are no errors. I am on Ubuntu 13.4 and using your file verbatim.

You cannot use it verbatim, you must change the SERVER_IP_ADDRESS value to the correct IP of your server.

If that isn't the problem, it may be an issue with using 13.04. This article is written for 12.04 because it is the most recent LTS and that is all I'll run on servers. Can you check the Apache logs to determine what is causing the error? You should be able to get a more detailed error message.


I have tried your above method but still i got "server not found error". I have done my configuration part correctly and the configuration part has copied below for your reference.

Kindly check with this and update the resolution for this issue