3

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
030
  • 5,901
  • 13
  • 68
  • 110
widyakumara
  • 141
  • 1
  • 1
  • 4

1 Answers1

6

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.

030
  • 5,901
  • 13
  • 68
  • 110
  • 1
    Wouldn't this trigger a overlap with the first declared vhost winning? – Phunky Nov 27 '14 at 15:12
  • 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 – carinlynchin Feb 28 '18 at 22:37