If you want to host several development projects at once, but are tired of creating hostnames and directories for every single project, use a setup like this. It uses mod_vhost_alias and requires a wildcard DNS entry, like *.dev.example.com for example. For completeness, create dev.example.com too.

Redirect dev.example.com to www.dev.example.com. That will put it in the catch-all vhostalias virtualhost:

<VirtualHost *:80>
  ServerName dev.example.com
  Redirect permanent / http://www.dev.example.com/
</VirtualHost>

A virtualhost needs a valid server name, so we put that here. Include the wildcard here, too.

<VirtualHost *:80>
  ServerName www.dev.example.com
  ServerAlias *.dev.example.com

  # %-4+ gives you the 4th part of the hostname from the end, and everything preceding that.
  # www.dev.example.com becomes www
  # sub.domain.dev.example.com becomes sub.domain
  VirtualDocumentRoot /www/dev.example.com/%-4+/htdocs/
  CustomLog /var/log/apache2/dev.example.com-access.log combined
  ErrorLog /var/log/apache2/dev.example.com-error.log

  # like above, %-4+ gives you the 4th part of the hostname from the end, plus everything before that.
  # this will setup a /cgi-bin/ ScriptAlias for every vhostalias.
  VirtualScriptAlias /www/dev.example.com/%-4+/cgi-bin/

  # specify options and overrides here.
  <Directory /www/dev.example.com/>
    Options None
    AllowOverride None
    Order deny,allow
  </Directory>
</VirtualHost>

Now, all you need to do is create /www/dev.example.com/www/htdocs/ and /www/dev.example.com/www/cgi-bin/ to get started. From then on, whenever you need a new hostname, just create the appropriate directories.

  • No labels