Failover patch for mod_vhost_alias
I use mod_vhost_alias extensively on my servers; it allows the dynamic creation of new virtual hosts without reloading apache. Instead, the incoming request’s ServerName is used to derive the path that the server should use as the DocumentRoot.
However, this had a major problem, at least for my purposes: if the path did not exist, it would generate a 404. I wanted it to fail over to another default DocumentRoot, which had a nice website explaining that the requested site didn’t exist (or perhaps doing something even more interesting, like providing a list of sites that DID exist). There was no option to do this in mod_vhost_alias, so I created the following patch.
This patch will cause mod_vhost_alias to check whether the transformed VirtualDocumentRoot exists for any given request; if it does not, then it will failover to the DocumentRoot instead. For this to work, you should define both VirtualDocumentRoot AND DocumentRoot for the same
Please note that I make no warranty about the proper functioning of this code. Use it at your own risk. I’m not responsible for any damage that results from using this.
Installation instructions:
- Make sure you have apache2 build tools. In debian,
apt-get install apache2-dev - Get an apache 2.2.3 source tree. On debian,
apt-get source apache2 - Go to modules/mappers in the source tree.
- Patch the module:
patch mod_vhost_alias.c /path/to/mod_vhost_alias_failover.patch - Build the patched module:
apxs2 -c mod_vhost_alias.c - Now you have a new mod_vhost_alias.so in .libs under your current directory. Install this over your existing one (which you should probably back up) and you’re done.