Using wildcards in Apache server aliases

Apache allows you to create hundreds of virtual host containers. Each container is required to have a ServerName directive, which contains the domain name associated with the virtual host. In addition to a server name, one ore more aliases can be associated with the virtual host with the ServerAlias directive. Aliases can contain a domain, or a regex that allows you to match based on some specific criteria. This is super useful, and allows you to do things like this:

NameVirtualHost 192.168.1.18:8080

<VirtualHost 192.168.1.18:8080>
     ServerName foo.com
     ServerAlias *.foo.com
</VirtualHost>

<VirtualHost 192.168.1.18:8080>
     ServerName bar.com
     ServerAlias *.bar.com
</VirtualHost>

This opens a whole slew of cool and interesting possibilities for virtual hosting. Niiiiice!

3 Comments

moo  on January 8th, 2007

Another great Apache virtual hosting feature is Dynamically Configured Mass Virtual Hosting.
http://httpd.apache.org/docs/2.0/vhosts/mass.html
It allows you to keep your apache config small and simple and add vhosts without changing the config or even restarting/reloading apache. I used to have this in place on a webserver I ran and it was excellent.

Bachsau  on February 14th, 2011

Wildcards are far away from beeing regular expressions…

Fatih  on August 24th, 2011

ignoring ServerName part applies this to all domains served. it is pretty helpful with WebMail apps.

Leave a Comment