Search code examples
apacheubuntuhostingvirtualmod-vhost-alias

Apache Virtual Host using mod_vhost_alias


I am trying to set up my apache module to dynamically direct all requests to a specific folder and then match the name to a folder of the same name.

To do this I set the following in my 000-default.conf file in the sites-available folder.

UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2

This worked great.

Then I wanted to setup a couple of different domains to not point to the example folder, but somewhere else, so I added a couple of these before the VirtualDocumentRoot line:

<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>

However, now the dynamic pointing does not work anymore and all the URL's are redirected to the first -> VirtualDocumentRoot location.

Can someone please indicate to me what I am doing wrong?

Full Code Example In apache2/sites-available/000-default.conf:

<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>

<VirtualHost *:80>
ServerName sub2.example.com
VirtualDocumentRoot /var/www/sub2.example.com
</VirtualHost>

<VirtualHost *:80>
ServerName sub3.example.com
VirtualDocumentRoot /var/www/sub3.example.com
</VirtualHost>

UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2


Solution

  • Do not use VirtualDocumentRoot for simple Virtualhosts, use only DocumentRoot.

    VirtualDocumentRoot defines the mass-virtualhost catch-all, and by definition you can only have one mass-virtualhost (else how could apache knows which VH a given hostname should match).

    Edit:

    Now you need some other changes: - ensure you have NameVirtualHost *:80 somewhere in apache configuration (unless you use Apache 2.4). - Move the Mass-Virtualhost as first, so it will become the default virtualhost. The default virtualhost is used when the request host name does not match any ServerName directive. (You can check the default VH by running apache with -S option).