apachevirtualhostapache-configvirtual-hosts

Setup Dynamic Virtual Host (Apache2 on Ubuntu)


I want to set up a single virtual host that can dynamically handle all requests based on the hostname used to access it. If %{HTTP_HOST} could be used in a DocumentRoot, this is probably exactly what I want:

<VirtualHost *:80>
    ServerAdmin me@example.com

    DocumentRoot /var/www/live/%{HTTP_HOST}/public
    <Directory />
            Options FollowSymLinks
            AllowOverride None
    </Directory>
    <Directory /var/www/live/%{HTTP_HOST}/public>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
    </Directory>

    # Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
    LogLevel warn
    ErrorLog /var/www/live/%{HTTP_HOST}/logs/error.log
    CustomLog /var/www/live/%{HTTP_HOST}/logs/access.log combined
</VirtualHost>

...unfortunately, %{HTTP_HOST} is not allowed in the DocumentRoot (Warning: DocumentRoot [/var/www/live/%{HTTP_HOST}/public] does not exist). How else can I achieve my goal?

Update: I thought of pointing a catch-all vhost to a single directory and having a .htaccess use mod_rewrite to dynamically select the path but (honestly) I'm exhausted. I'll try at it again in the morning, but in the meantime, if anyone has good ideas, I'd love to hear them! Thank you!


Solution

  • The official methods for achieving dynamic virtual hosts are explained in the Apache documentation:

    http://httpd.apache.org/docs/2.0/vhosts/mass.html