apache2aliasvirtualhostmod-vhost-alias

mod_vhost_alias and VirtualDocumentRoot: How to add penultimate and last part?


How to obtain a directory name consisting of two last parts of the domain name. Or use a dot?

For example, I have a domain any.ms and I would like the dynamic domains foo.any.ms and domain bar.any.ms also pointing to the /var/www/dev/any.ms/public directory. Or domain any.lorem.ipsum.ms to point to the directory /var/www/dev/ipsum.ms/public.

My ms.conf for now (not working, apache won't start):

<VirtualHost *:80>
    ServerAlias *.ms
    VirtualDocumentRoot /var/www/dev/%-2%.%-1/public
    UseCanonicalName Off
    <Directory "/var/www/dev">
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

I use the documentation http://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html but I can not insert dots writing /%-2.%-1/, /%-2%.%-1/, /%-2\.%-1/.


Solution

  • I have just found a solution:

    If you want to include the . (dot) character in a VirtualDocumentRoot directive, but it crashes with a % directive, you can work around the problem in the following way:

    VirtualDocumentRoot /usr/local/apache/vhosts/%2.0.%3.0

    Working solution:

    <VirtualHost *:80>
        ServerAlias *.ms
        VirtualDocumentRoot /var/www/dev/%-2.0.%-1.0/public
        UseCanonicalName Off
        <Directory "/var/www/dev">
            Options FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
            Require all granted
        </Directory>
    </VirtualHost>