Jordan Savant # Software Engineer

Apache Virtual Alias

Apache Virtual Alias lets you map a subdomain to a directory within a Virtual Host entry. This lets you create dynamic sites just from directories without needing to create a virtual host entry each time.

Enable Mod

sudo a2enmod vhost_alias

VirtualHost Example

<VirtualHost *:80>
    ServerAlias *.acme.com
    VirtualDocumentRoot /home/user/sites/%1
    <Directory /home/user/sites>
        Options +Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Once this is setup restart apache and all subdomains will map to relevant document roots.

Its also possible to map more than one wildcard to a subdomain, which matches in order of subdomain

<VirtualHost *:80>
    ServerAlias *.*.acme.com
    VirtualDocumentRoot /home/user/sites/%2/%1
    <Directory /home/user/sites>
        Options +Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>