Web Application Developer.


More ModRewrite - Using a dbm file instead of a txt file

So i'm using ModRewrite to do a 1-to-1 forwarding list of urls on a website where my cms can write to the map file automatically (so we don't have to edit httpd.conf every time we want to add or remove an entry).

The problem is that if the text file gets long enough (probably way longer than mine is, but i'm an efficiency freak, and having a hundred lookups before the user ever gets to your page is excessive).

So I decided to use a .dbm file (which is a hashed file of 1-to-1 matched phrases, Dbm on wikipedia). The problem is that the documentation for ModRewrite really sucks sometimes, and it doesn't explain fully how to get all the way through the process, which leaves you guessing sometimes.

The most confusing thing is when you use the perl script on the official apache site here it's written for the NDBM format (which isn't available on my box).

Here's the perl script:

#!/path/to/bin/perl
##
##  txt2dbm -- convert txt map to dbm format
##

use NDBM_File;
use Fcntl;

($txtmap, $dbmmap) = @ARGV;

open(TXT, "<$txtmap") or die "Couldn't open $txtmap!\n";
tie (%DB, 'NDBM_File', $dbmmap,O_RDWR|O_TRUNC|O_CREAT, 0644)
  or die "Couldn't create $dbmmap!\n";

while () {
  next if (/^\s*#/ or /^\s*$/);
  $DB{$1} = $2 if (/^\s*(\S+)\s+(\S+)/);
}

untie %DB;
close(TXT); 

So after some trial and error, I found out that my version of apache works with SDBM, and it appears that if you just change NDBM to SDBM it works...

#!/usr/bin/perl -w
##
##  txt2dbm -- convert txt map to dbm format
##

use SDBM_File;
use Fcntl;

($txtmap, $dbmmap) = @ARGV;

open(TXT, "<$txtmap") or die "Couldn't open $txtmap!\n";
tie (%DB, 'SDBM_File', $dbmmap,O_RDWR|O_TRUNC|O_CREAT, 0644)
  or die "Couldn't create $dbmmap!\n";

while () {
  next if (/^\s*#/ or /^\s*$/);
  $DB{$1} = $2 if (/^\s*(\S+)\s+(\S+)/);
}

untie %DB;
close(TXT);

So problem 1 fixed. Then I find out the output of this thing is two files instead of one...wtf? The files are named: rewritemap.dbm.dir and rewritemap.dbm.pag

Here's the short answer: If you just ignore the additional extensions, it will work. Here's the httpd.conf line:

RewriteMap redirects dbm:/.../.../.../rewrite/rewritemap.dbm


Posted by Jeremy Tunnell on September 15th 7:36 PM

Tags




 

Comments


Great article! Thanks for clearing up this otherwise poorly-documented issue.

Posted by Nick on October 18th 01:41:19 AM



Yes, thank you very much. I ran in to the exact same problem. From what I gather you can only use

use NDBM_File;

With Perl 5.10

And I couldn't upgrade my system to this version.

Many thanks!

Posted by Kevin Smith on July 31st 09:50:23 AM



One small typo.

while ()

Should be:

while (<TXT>)

-Peter

Posted by Peter Burkholder on April 09th 08:44:50 AM


Add a Comment