Wild card sub-domain routing in Apache

apache-2.2wildcard-subdomain

I've noticed some hosting providers have Apache setup so that if I create a directory (foo.example.com) Apache will automatically know that the DNS entry for foo.example.com routes to that document root.

What I am to do is setup something like this:

<VirtualHost *:80>
   ServerName *.example.com
   DocumentRoot /home/user/*.example.com
</VirtualHost>

Where the DocumentRoot would match based on the pattern from the ServerName wild card.

In doing so all I should need to do is create /home/user/foo.example.com after the configuration is in place and not need to modify anything in Apache. This would allow me to add sub-domains on the fly without needing to restart or reload or even edit anything in Apache.

A use case would be something like this.

  1. mkdir /home/user/baz.example.com/
  2. ?????
  3. Profit

Where I wouldn't need to do anything but simply make the directory.

Best Answer

VirtualDocumentRoot should do the trick.

<VirtualHost *:80>
    ServerName catchall.invalid
    ServerAlias *
    VirtualDocumentRoot /home/user/%0
</VirtualHost>
Related Topic