From: Rich Bowen Date: Wed, 19 May 2010 22:00:50 +0000 (+0000) Subject: Completes the rewrite of the RewriteMap documentation. Examples for all X-Git-Tag: 2.3.6~97 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=326640caaa864a686320094c945e4fd7adc43566;p=apache Completes the rewrite of the RewriteMap documentation. Examples for all the various map types, and an overhaul from a grammar perspective. Still want to do some tweaks on the ref doc side. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@946438 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/docs/manual/rewrite/rewritemap.html.en b/docs/manual/rewrite/rewritemap.html.en index 6c89ecf781..8ca34bf7ce 100644 --- a/docs/manual/rewrite/rewritemap.html.en +++ b/docs/manual/rewrite/rewritemap.html.en @@ -103,6 +103,17 @@ in the map:

RewriteRule ^/ex/(.*) ${examplemap:$1|/not_found.html}

+

Per-directory and .htaccess context

+

+The RewriteMap directive may not be used in +<Directory> sections or .htaccess files. You must +declare the map in server or virtualhost context. You may use the map, +once created, in your RewriteRule and +RewriteCond directives in those scopes. You just can't +declare it in those scopes. +

+
+

The sections that follow describe the various MapTypes that may be used, and give examples of each.

top
@@ -170,6 +181,15 @@ telephone 328

+

Cached lookups

+

+ 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. +

+
+
top

rnd: Randomized Plain Text

@@ -262,6 +282,15 @@ a common base name. For example, you may have two files named mapfile.map.dir and mapfiile.map.pag. This is normal, and you need only use the base name mapfile.map in your RewriteMap directive.

+
+ +

Cached lookups

+

+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. +

top
@@ -309,79 +338,108 @@ your RewriteMap directive.

top

prg: External Rewriting Program

- -

MapType: prg, MapSource: Unix filesystem path to valid regular file

- -

Here the source is a program, not a map file. To -create it you can use a language of your choice, but -the result has to be an executable program (either -object-code or a script with the magic cookie trick -'#!/path/to/interpreter' as the first -line).

This program is started once, when the Apache httpd server -is started, and then communicates with the rewriting engine -via its stdin and stdout -file-handles. For each map-function lookup it will -receive the key to lookup as a newline-terminated string -on stdin. It then has to give back the -looked-up value as a newline-terminated string on -stdout or the four-character string -``NULL'' if it fails (i.e., there -is no corresponding value for the given key).

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 -Mutex directive.

External rewriting programs are not started if they're defined in a -context that does not have RewriteEngine set to -on

. - -

A trivial program which will implement a 1:1 map (i.e., - key == value) could be:

-#!/usr/bin/perl
-$| = 1;
-while (<STDIN>) {
-    # ...put here any transformations or lookups...
-    print $_;
-}
-

But be very careful:

  1. ``Keep it simple, stupid'' (KISS). -If this program hangs, it will cause Apache httpd to hang -when trying to use the relevant rewrite rule.
  2. A common mistake is to use buffered I/O on -stdout. Avoid this, as it will cause a deadloop! -``$|=1'' is used above, to prevent this.
top
+ +

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 RewriteEngine set to + on.

+ +

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 + Mutex directive.

+ +

A simple example is shown here which will replace all dashes with + underscores in a request URI.

+ +

Rewrite configuration

+ RewriteMap d2u prg:/www/bin/dash2under.pl
+ RewriteRule - ${d2u:%{REQUEST_URI}} +

+ +

dash2under.pl

+ #!/usr/bin/perl
+ $| = 1; # Turn off I/O buffering
+ while (<STDIN>) {
+ + s/-/_/g; # Replace dashes with underscores
+ print $_;
+
+ }
+

+ +

Caution!

+ +
+ +
top

dbd or fastdbd: SQL Query

-

MapType: dbd or fastdbd, -MapSource: An SQL SELECT statement that takes a single - argument and returns a single value.

-

This uses mod_dbd to implement a rewritemap -by lookup in an SQL database. There are two forms: -fastdbd caches database lookups internally, -dbd doesn't. So dbd incurs a -performance penalty but responds immediately if the database -contents are updated, while fastdbd is more -efficient but won't re-read database contents until server -restart.

+ +

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.

+ +

mod_dbd will need to be configured to point at + the right database for this statement to be executed.

+ +

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.

+

Example

RewriteMap myquery "fastdbd:SELECT destination FROM rewrite WHERE source = %s" -

+

+
top

Summary

+

The RewriteMap directive can occur more than - once. For each mapping-function use one - RewriteMap directive to declare its rewriting - mapfile. While you cannot declare a map in - per-directory context it is of course possible to - use this map in per-directory context.

-

Note

For plain text and DBM format files the -looked-up keys are cached in-core until the mtime of the -mapfile changes or the server does a restart. This way you can have -map-functions in rules which are used for every -request. This is no problem, because the external lookup only happens -once! -
+ once. For each mapping-function use one + RewriteMap directive to declare its rewriting + mapfile.

+ +

While you cannot declare a map in + per-directory context (.htaccess files or + <Directory> blocks) it is possible to + use this map in per-directory context.

+

Available Languages:  en 

diff --git a/docs/manual/rewrite/rewritemap.xml b/docs/manual/rewrite/rewritemap.xml index 119ae676ee..de53017cf2 100644 --- a/docs/manual/rewrite/rewritemap.xml +++ b/docs/manual/rewrite/rewritemap.xml @@ -102,6 +102,17 @@ in the map:

RewriteRule ^/ex/(.*) ${examplemap:$1|/not_found.html} +Per-directory and .htaccess context +

+The RewriteMap directive may not be used in +<Directory> sections or .htaccess files. You must +declare the map in server or virtualhost context. You may use the map, +once created, in your RewriteRule and +RewriteCond directives in those scopes. You just can't +declare it in those scopes. +

+
+

The sections that follow describe the various MapTypes that may be used, and give examples of each.

@@ -171,6 +182,15 @@ telephone 328 + Cached lookups +

+ 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. +

+
+
rnd: Randomized Plain Text @@ -265,6 +285,15 @@ a common base name. For example, you may have two files named mapfile.map.dir and mapfiile.map.pag. This is normal, and you need only use the base name mapfile.map in your RewriteMap directive.

+ + +Cached lookups +

+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. +

@@ -312,76 +341,108 @@ your RewriteMap directive.

prg: External Rewriting Program - -

MapType: prg, MapSource: Unix filesystem path to valid regular file

- -

Here the source is a program, not a map file. To -create it you can use a language of your choice, but -the result has to be an executable program (either -object-code or a script with the magic cookie trick -'#!/path/to/interpreter' as the first -line).

This program is started once, when the Apache httpd server -is started, and then communicates with the rewriting engine -via its stdin and stdout -file-handles. For each map-function lookup it will -receive the key to lookup as a newline-terminated string -on stdin. It then has to give back the -looked-up value as a newline-terminated string on -stdout or the four-character string -``NULL'' if it fails (i.e., there -is no corresponding value for the given key).

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 -Mutex directive.

External rewriting programs are not started if they're defined in a -context that does not have RewriteEngine set to -on

. - -

A trivial program which will implement a 1:1 map (i.e., - key == value) could be:

-#!/usr/bin/perl
-$| = 1;
-while (<STDIN>) {
-    # ...put here any transformations or lookups...
-    print $_;
-}
-

But be very careful:

  1. ``Keep it simple, stupid'' (KISS). -If this program hangs, it will cause Apache httpd to hang -when trying to use the relevant rewrite rule.
  2. A common mistake is to use buffered I/O on -stdout. Avoid this, as it will cause a deadloop! -``$|=1'' is used above, to prevent this.
+ +

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 RewriteEngine set to + on.

+ +

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 + Mutex directive.

+ +

A simple example is shown here which will replace all dashes with + underscores in a request URI.

+ + Rewrite configuration + RewriteMap d2u prg:/www/bin/dash2under.pl
+ RewriteRule - ${d2u:%{REQUEST_URI}} +
+ + dash2under.pl + #!/usr/bin/perl
+ $| = 1; # Turn off I/O buffering
+ while (<STDIN>) {
+ + s/-/_/g; # Replace dashes with underscores
+ print $_;
+
+ }
+
+ +Caution! +
    +
  • Keep your rewrite map program as simple as possible. If the program +hangs, it will cause httpd to wait indefinitely for a response from the +map, which will, in turn, cause httpd to stop responding to +requests.
  • +
  • Be sure to turn off buffering in your program. In Perl this is done +by the second line in the example script: $| = 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.
  • +
  • Remember that there is only one copy of the program, started at +server startup. All requests will need to go through this one bottleneck. +This can cause significant slowdowns if many requests must go through +this process, or if the script itself is very slow.
  • +
+
+ + + +
dbd or fastdbd: SQL Query -

MapType: dbd or fastdbd, -MapSource: An SQL SELECT statement that takes a single - argument and returns a single value.

-

This uses mod_dbd to implement a rewritemap -by lookup in an SQL database. There are two forms: -fastdbd caches database lookups internally, -dbd doesn't. So dbd incurs a -performance penalty but responds immediately if the database -contents are updated, while fastdbd is more -efficient but won't re-read database contents until server -restart.

+ +

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.

+ +

mod_dbd will need to be configured to point at + the right database for this statement to be executed.

+ +

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.

+ Example RewriteMap myquery "fastdbd:SELECT destination FROM rewrite WHERE source = %s" - + +
Summary +

The RewriteMap directive can occur more than - once. For each mapping-function use one - RewriteMap directive to declare its rewriting - mapfile. While you cannot declare a map in - per-directory context it is of course possible to - use this map in per-directory context.

- Note For plain text and DBM format files the -looked-up keys are cached in-core until the mtime of the -mapfile changes or the server does a restart. This way you can have -map-functions in rules which are used for every -request. This is no problem, because the external lookup only happens -once! - + once. For each mapping-function use one + RewriteMap directive to declare its rewriting + mapfile.

+ +

While you cannot declare a map in + per-directory context (.htaccess files or + <Directory> blocks) it is possible to + use this map in per-directory context.

+