This document supplements the
The
The syntax of the
The MapName is an arbitrary name that you assign to the map, and which you will use in directives later on. Arguments are passed to the map via the following syntax:
${
MapName :
LookupKey
}
${
MapName :
LookupKey |
DefaultValue }
When such a construct occurs, the map MapName is consulted and the key LookupKey is looked-up. If the key is found, the map-function construct is substituted by SubstValue. If the key is not found then it is substituted by DefaultValue or by the empty string if no DefaultValue was specified.
For example, you can define a
You would then be able to use this map in a
A default value can be specified in the event that nothing is found in the map:
The .htaccess
files. You must
declare the map in server or virtualhost context. You may use the map,
once created, in your
The sections that follow describe the various MapTypes that may be used, and give examples of each.
When a MapType of int
is used, the MapSource is one
of the available internal ap_register_rewrite_mapfunc
API.
The functions that are provided by default are:
To use one of these functions, create a
Redirect a URI to an all-lowercase version of itself
Please note that the example offered here is for
illustration purposes only, and is not a recommendation. If you want
to make URLs case-insensitive, consider using
When a MapType of txt
is used, the MapSource is a filesystem path to a
plain-text mapping file, containing one space-separated key/value pair
per line. Optionally, a line may contain a comment, starting with
a '#' character.
A valid text rewrite map file will have the following syntax:
When the
For example, we can use a mapfile to translate product names to product IDs for easier-to-remember URLs, using the following recipe:
Product to ID configuration
We assume here that the prods.php
script knows what
to do when it received an argument of id=NOTFOUND
when
a product is not found in the lookup map.
The file /etc/apache2/productmap.txt
then contains
the following:
Thus, when http://example.com/product/television
is
requested, the /prods.php?id=993
.
.htaccess
file, you'll need to remove the leading slash from the rewrite
pattern in order for it to match anything:
The looked-up keys are cached by httpd until the mtime
(modified time) of the mapfile changes, or the httpd server is
restarted. This ensures better performance on maps that are called
by many requests.
When a MapType of rnd
is used, the MapSource is a
filesystem path to a plain-text mapping file, each line of which
contains a key, and one or more values separated by |
.
One of these values will be chosen at random if the key is
matched.
For example, you can use the following map file and directives to provide a random load balancing between several back-end servers, via a reverse-proxy. Images are sent to one of the servers in the 'static' pool, while everything else is sent to one of the 'dynamic' pool.
Configuration directives
So, when an image is requested and the first of these rules is
matched, static
in the map file, which returns one of the
specified hostnames at random, which is then used in the
If you wanted to have one of the servers more likely to be chosen (for example, if one of the server has more memory than the others, and so can handle more requests) simply list it more times in the map file.
When a MapType of dbm
is used, the MapSource is a
filesystem path to a DBM database file containing key/value pairs to
be used in the mapping. This works exactly the same way as the
txt
map, but is much faster, because a DBM is indexed,
whereas a text file is not. This allows more rapid access to the
desired key.
You may optionally specify a particular dbm type:
The type can be sdbm
, gdbm
, ndbm
or db
.
However, it is recommended that you just use the httxt2dbm utility that is
provided with Apache HTTP Server, as it will use the correct DBM library,
matching the one that was used when httpd itself was built.
To create a dbm file, first create a text map file as described
in the txt section. Then run
httxt2dbm
:
You can then reference the resulting file in your
Note that with some dbm types, more than one file is generated, with
a common base name. For example, you may have two files named
mapfile.map.dir
and mapfile.map.pag
. This is
normal, and you need only use the base name mapfile.map
in
your
The looked-up keys are cached by httpd until the mtime
(modified time) of the mapfile changes, or the httpd server is
restarted. This ensures better performance on maps that are called
by many requests.
When a MapType of prg
is used, the MapSource is a
filesystem path to an executable program which will providing the
mapping behavior. This can be a compiled binary file, or a program
in an interpreted language such as Perl or Python.
This program is started once, when the Apache HTTP Server is
started, and then communicates with the rewriting engine via
STDIN
and STDOUT
. That is, for each map
function lookup, it expects one argument via STDIN
, and
should return one new-line terminated response string on
STDOUT
. If there is no corresponding lookup value, the
map program should return the four-character string
"NULL
" to indicate this.
External rewriting programs are not started if they're defined in
a context that does not have on
.
By default, external rewriting programs are run as the
user:group who started httpd. This can be changed on UNIX systems
by passing user name and group name as third argument to
username:groupname
format.
This feature utilizes the rewrite-map
mutex,
which is required for reliable communication with the program.
The mutex mechanism and lock file can be configured with the
A simple example is shown here which will replace all dashes with underscores in a request URI.
Rewrite configuration
dash2under.pl
$| = 1;
This will
of course vary in other languages. Buffered I/O will cause httpd to wait
for the output, and so it will hang.When a MapType of dbd
or fastdbd
is
used, the MapSource is a SQL SELECT statement that takes a single
argument and returns a single value.
There are two forms of this MapType.
Using a MapType of dbd
causes the query to be
executed with each map request, while using fastdbd
caches the database lookups internally. So, while
fastdbd
is more efficient, and therefore faster, it
won't pick up on changes to the database until the server is
restarted.
If a query returns more than one row, a random row from the result set is used.
The query name is passed to the database driver as a label for an SQL prepared statement, and will therefore need to follow any rules (such as case-sensitivity) required for your database.
The
While you cannot declare a map in
per-directory context (.htaccess
files or