httxt2dbm

For many years, the mod_rewrite documentation contained an example Perl program that could, it claimed, take a simple key:value map file, and convert it to a DBM file, for use with the RewriteMap directive. This allowed for fast lookup of values for use with one-to-one lookup-based rewrite rules.

There was just one problem with this script. It didn’t always work, and it relied on certain assumptions about the DBM library with which you had built Apache.

This was very unfortunate, since the RewriteMap directive is extremely powerful, but seldom used due to this flaw, along with poor documentation of the RewriteMap feature.

With the 2.2 release, there is a new program which is installed in the bin directory, called httxt2dbm. This takes a text file and converts it to a DBM file, using the same DBM library that was used when Apache was built. This guarantees that mod_rewrite can read this file without any difficulty.

The format of the text file is as follows:

pattern1    target1
pattern2    target2

Assuming that you save this file as rules.in, you can convert it to a DBM using the following command line:

httxt2dbm -i rules.in -o rules.map

You can then use this map file in a rewrite ruleset using the following syntax:

RewriteMap mapname dbm:/www/conf/rules.map

RewriteEngine On
RewriteRule ^/example/(.*) /otherplace/${mapname:$1}

This configuration will cause http://servername/example/pattern1 to be rewritten to http://servername/otherplace/target1 and http://servername/example/pattern2 ...

Get What's New in Apache Web Server 2.2? now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.