How to configure and combine wildcard domains and subdomains in Apache2's VirtualDocumentRoot?

10,432

According to this documentation multiple parts of a FQDN could be used. In your configuration you are using %1 which corresponds the subdomain. If the FQDN is img.alpha.dev then %1 would result in img and as %2 corresponds to the domain it results in alpha and %0 to img.alpha.dev as %0 corresponds to FQDN.

Implementing the following configuration:

<VirtualHost *:80>
    ServerAlias *
    VirtualDocumentRoot /var/www/%2/%1
</VirtualHost>

in /etc/httpd/conf/httpd.conf, executing mkdir -p /var/www/mydomain/info && echo helloworld > /var/www/mydomain/info/index.html, restarting httpd and navigating to http://info.mydomain.com/ results in helloworld.

However if a ServerAlias is specified more, e.g.:

<VirtualHost *:80>
    ServerAlias *.mydomain.com
    VirtualDocumentRoot /var/www/%1
</VirtualHost>  

executing mkdir -p /var/www/info && echo test2_param_one > /var/www/info/index.html and navigating to info.mydomain.com results in test2_param_one.

<VirtualHost *:80>
    ServerAlias *.mydomain.com
    VirtualDocumentRoot /var/www/%0
</VirtualHost>

executing mkdir -p /var/www/info.mydomain.com && echo test2_param_all > /var/www/info.mydomain.com/index.html and navigating to info.mydomain.com results in test2_param_all.

In conclusion, the following snippet works:

<VirtualHost *:80>
    ServerAlias *.*.dev
    VirtualDocumentRoot /var/www/%2/%1
</VirtualHost>
<VirtualHost *:80>
    ServerAlias *.dev
    VirtualDocumentRoot /var/www/%1/test
</VirtualHost>

mkdir -p /var/www/alpha/test && echo test5 > /var/www/alpha/test/index.html, mkdir -p /var/www/alpha/img && echo test10 > /var/www/alpha/img/index.html, navigating to http://alpha.dev/ returns test5 and navigating to http://img.alpha.dev/ returns test10.

Share:
10,432

Related videos on Youtube

widyakumara
Author by

widyakumara

Updated on September 18, 2022

Comments

  • widyakumara
    widyakumara over 1 year

    httpd-vhosts.conf

    <Directory "/Volumes/DATA/websites">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    
    <Virtualhost *:80>
        VirtualDocumentRoot "/Volumes/DATA/websites/default"
        ServerName default.dev
        UseCanonicalName Off
    </Virtualhost>
    
    <Virtualhost *:80>
        VirtualDocumentRoot "/Volumes/DATA/websites/%1/www"
        ServerAlias *.dev
        UseCanonicalName Off
    </Virtualhost>
    

    Requests are currently handled as follows:

    alpha.dev   -> /Volumes/DATA/websites/alpha/www
    beta.dev    -> /Volumes/DATA/websites/beta/www
    

    Is it possible to have wildcard domains and subdomains? E.g.:

    alpha.dev       -> /Volumes/DATA/websites/alpha/www
    img.alpha.dev   -> /Volumes/DATA/websites/alpha/img
    cdn.alpha.dev   -> /Volumes/DATA/websites/alpha/cdn
    
    beta.dev        -> /Volumes/DATA/websites/beta/www
    docs.beta.dev   -> /Volumes/DATA/websites/beta/docs
    blog.beta.dev   -> /Volumes/DATA/websites/beta/blog
    
  • Phunky
    Phunky over 9 years
    Wouldn't this trigger a overlap with the first declared vhost winning?
  • carinlynchin
    carinlynchin about 6 years
    I looked at the doc.. i'm new to the server stuff... developer by trade by just haven't gotten into server stuff. What is the explanation between %0, %1... etc