[ Pobierz całość w formacie PDF ]
then mod_rewrite will have to read through the entire file, one line at a time, every time
that URL is requested. As the file grows, so does the average time taken to do a rewrite.
The solution to this problem is to use an indexed file, such as a DBM file. This allows
for much larger lists of rewrite mappings without any loss of performance. Using a dbm:
map type allows you to do this.1
1. The dbm:map type, rather than the txt:map type, is what is being used to provide the documentation
rewrite service described in the previous txt:section, at http://fajita.drbacchus.com/. So, if you
point your browser to http://fajita.drbacchus.com/allowoverride, you will receive the Apache doc-
umentation for the AllowOverrideconfiguration directive.
5610_c06_final.qxd 1/10/06 1:10 AM Page 65
CHAPTER 6 % THE REWRITEMAP DIRECTIVE 65
First, though, we need to generate that DBM file. We re going to create our map file in
exactly the same way we did in the case of the txt:map files. That is, we ll have a list of
keys and values, separated by one or more spaces. But once that map file is generated,
we ll need to generate a DBM file from that map using the httxt2dbmutility:
httxt2dbm -i rewrite.map -o rewrite.dbm
Invoke httxt2dbmwith no arguments to see more detailed instructions about how to
use it:
httxt2dbm -- Program to Create DBM Files for use by RewriteMap
Usage: httxt2dbm [-v] [-f format] -i SOURCE_TXT -o OUTPUT_DBM
Options:
-v More verbose output
-i Source Text File. If '-', use stdin.
-o Output DBM.
-f DBM Format. If not specified, will use the APR Default.
GDBM for GDBM files (unavailable)
SDBM for SDBM files (available)
DB for berkeley DB files (unavailable)
NDBM for NDBM files (unavailable)
default for the default DBM type
There are a number of different DBM formats, depending on the particular DBM
library or libraries you happen to have installed. It is important that you use the same
DBM library as was used to build Apache, so that the format is understandable to
Apache.
If you are running a version of Apache that does not have the httxt2dbmutility
available (any version before 2.0.56), there is a Perl program listing in the documentation
that has a similar effect.
Once you ve generated the DBM file, you can now point your RewriteMapdirective
at this newly created database with the following:
RewriteMap dbmrewritemap dbm:/path/to/rewrite.dbm
The dbmrewritemaprewrite function is now usable in RewriteRules:
RewriteRule /apachedocs/(.*) ${dbmrewritemap:$1} [R]
5610_c06_final.qxd 1/10/06 1:10 AM Page 66
66 CHAPTER 6 % THE REWRITEMAP DIRECTIVE
External Programs
Sometimes the rewrite that you want to do is sufficiently complicated that you just want
to write a program to handle it for you. The prg:rewrite map type lets you do exactly
that. The example given here will be in Perl, because Perl is exceptionally good at han-
dling text string manipulation, but you can write a rewrite program in any language at all.
The syntax for using this map type is identical to the other map types. The file path
points to the location of the script or program you wish to execute to perform the rewrite.
In the following example, we ll assume that someone copied a bunch of URLs incor-
rectly and replaced underscores with dashes in a brochure that talks about our website.
In order to compensate for this typo, we ll need to make sure that the URLs in the
brochure work, but also that the existing (correct) URLs all work, since existing links
to our pages need to continue to work.
Doing this with just RewriteRules would be cumbersome. A URL might contain one
dash or ten, and it s just easier to call on Perl to do this than try to monkey with [N]rules.
The script to do this translation in Perl is as follows:
#!/usr/bin/perl
$| = 1; # Turn off buffering
while () {
s/-/_/g; # Replace - with _ globally
print $_;
}
Buffering is turned off so that the response is returned immediately. The program
is started up when Apache starts up and is kept running for the lifetime of the Apache
process. It is therefore important to keep our rewrite scripts as simple as possible.
A rewrite program that hangs or crashes may cause the containing Apache process
[ Pobierz całość w formacie PDF ]