Anton Lindstrom (about, @twitter, @github)

Apache rewrite maps

Published:

At $WORK we have a couple of domains for our clients and need to redirect them to a specific sub-URL. Previously we've done this with a script which required some manual work and unfortunately app downtime.

We wanted to implement SSL terminators in front of our app servers and decided that Apache would be a good fit. Hearing that Fastly praises Apache for it's speed, it was decided to be put up to a test. By using Apache we also got a nifty little function called RewriteMaps.

For example, a customer domain can look like http://antonlindstrom.com and should redirect to http://example.com/anton.

To enforce this for over 100 domains we used the following Rewrite semantics:

RewriteEngine   On

RewriteMap      exampledomain           txt:/etc/apache2/domains.txt
RewriteRule     ^/$                     ${exampledomain:%{HTTP_HOST}}

RewriteLog      /var/log/apache2/rewrite.log
RewriteLogLevel 5

In the file /etc/apache2/domains.txt we can add the following:

antonlindstrom.com   http://example.com/anton
exampledomain.com    http://example.com/exampledomain

When using RewriteMaps it's possible to add domains on the fly without restarting or reloading services. It's a simple solution on a simple problem. I'm really happy about solving it with a simple and off the shelf solution with having room to grow with database backends.