From: Rich Bowen Date: Tue, 18 May 2010 23:46:08 +0000 (+0000) Subject: Adds a useful and complete example for using RewriteMap txt: X-Git-Tag: 2.3.6~109 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8aef31c0df5ad33d3c1b469f354a04d6b4fb5491;p=apache Adds a useful and complete example for using RewriteMap txt: git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@945966 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/docs/manual/rewrite/rewritemap.html.en b/docs/manual/rewrite/rewritemap.html.en index 7da69d414d..1557b3c964 100644 --- a/docs/manual/rewrite/rewritemap.html.en +++ b/docs/manual/rewrite/rewritemap.html.en @@ -92,9 +92,16 @@ RewriteMap MapName MapType:MapSource

You would then be able to use this map in a RewriteRule as follows:

-

- RewriteRule ^/ex/(.*) ${examplemap:$1} -

+

+ RewriteRule ^/ex/(.*) ${examplemap:$1} +

+ +

A default value can be specified in the event that nothing is found +in the map:

+ +

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

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

@@ -102,30 +109,67 @@ may be used, and give examples of each.

txt: Plain text maps

-

MapType: txt, MapSource: Unix filesystem - path to valid regular file

-

This is the standard rewriting map feature where the - MapSource is a plain ASCII file containing - either blank lines, comment lines (starting with a '#' - character) or pairs like the following - one per - line.

+ +

When a MapType of txtis used, the MapSource is a filesystem path to a + plain-text mapping file, containing space-separated key/value pair + per line. Optionally, a line may be contain a comment, starting with + a '#' character.

+ +

For example, the following might be valid entries in a map + file.

+

- - MatchingKey - SubstValue - + # Comment line + MatchingKey SubstValue
+ MatchingKey SubstValue # comment

-

Example

+
+    

When the RewriteMap is invoked the argument is looked for in the + first argument of a line, and, if found, the substitution value is + returned.

+ +

For example, we might use a mapfile to translate product names to + product IDs for easier-to-remember URLs, using the following + recipe:

+ +

Product to ID configuration

+ RewriteMap product2id txt:/etc/apache2/productmap.txt
+ RewriteRule ^/product/(.*) /prods.php?id=${product2id:$1|NOTFOUND} [PT] +

+ +

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:

+ +

Product to ID map

 ##
-##  map.txt -- rewriting map
+##  productmap.txt - Product to ID map file
 ##
 
-Ralf.S.Engelschall    rse   # Bastard Operator From Hell
-Mr.Joe.Average        joe   # Mr. Average
+television 993
+stereo     198
+fishingrod 043
+basketball 418
+telephone  328
 
+ +

Thus, when http://example.com/product/television is + requested, the RewriteRule is applied, and the request + is internally mapped to /prods.php?id=993.

+ +

Note: .htaccess files

+ The example given is crafted to be used in server or virtualhost + scope. If you're planning to use this in a .htaccess + file, you'll need to remove the leading slash from the rewrite + pattern in order for it to match anything:

-RewriteMap real-to-user txt:/path/to/file/map.txt -

+ RewriteRule ^product/(.*) /prods.php?id=${product2id:$1|NOTFOUND} [PT] +

+
+
top

rnd: Randomized Plain Text

diff --git a/docs/manual/rewrite/rewritemap.xml b/docs/manual/rewrite/rewritemap.xml index f7f532bb2a..4173ab7fb5 100644 --- a/docs/manual/rewrite/rewritemap.xml +++ b/docs/manual/rewrite/rewritemap.xml @@ -91,9 +91,16 @@ RewriteMap MapName MapType:MapSource

You would then be able to use this map in a RewriteRule as follows:

- - RewriteRule ^/ex/(.*) ${examplemap:$1} - + + RewriteRule ^/ex/(.*) ${examplemap:$1} + + +

A default value can be specified in the event that nothing is found +in the map:

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

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

@@ -101,33 +108,69 @@ may be used, and give examples of each.

txt: Plain text maps -

MapType: txt, MapSource: Unix filesystem - path to valid regular file

-

This is the standard rewriting map feature where the - MapSource is a plain ASCII file containing - either blank lines, comment lines (starting with a '#' - character) or pairs like the following - one per - line.

+ +

When a MapType of txtis used, the MapSource is a filesystem path to a + plain-text mapping file, containing space-separated key/value pair + per line. Optionally, a line may be contain a comment, starting with + a '#' character.

+ +

For example, the following might be valid entries in a map + file.

+

- - MatchingKey - SubstValue - + # Comment line + MatchingKey SubstValue
+ MatchingKey SubstValue # comment

- - Example + +

When the RewriteMap is invoked the argument is looked for in the + first argument of a line, and, if found, the substitution value is + returned.

+ +

For example, we might use a mapfile to translate product names to + product IDs for easier-to-remember URLs, using the following + recipe:

+ + Product to ID configuration + RewriteMap product2id txt:/etc/apache2/productmap.txt
+ RewriteRule ^/product/(.*) /prods.php?id=${product2id:$1|NOTFOUND} [PT] +
+ +

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:

+ + Product to ID map
 ##
-##  map.txt -- rewriting map
+##  productmap.txt - Product to ID map file
 ##
 
-Ralf.S.Engelschall    rse   # Bastard Operator From Hell
-Mr.Joe.Average        joe   # Mr. Average
+television 993
+stereo     198
+fishingrod 043
+basketball 418
+telephone  328
 
+ +

Thus, when http://example.com/product/television is + requested, the RewriteRule is applied, and the request + is internally mapped to /prods.php?id=993.

+ + Note: .htaccess files + The example given is crafted to be used in server or virtualhost + scope. If you're planning to use this in a .htaccess + file, you'll need to remove the leading slash from the rewrite + pattern in order for it to match anything: -RewriteMap real-to-user txt:/path/to/file/map.txt - + RewriteRule ^product/(.*) /prods.php?id=${product2id:$1|NOTFOUND} [PT] +
+ +
rnd: Randomized Plain Text