From: Rich Bowen
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.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 txt
is 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
+ +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 ofid=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 328Thus, when
+ +http://example.com/product/television
is + requested, theRewriteRule
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:+ RewriteRule ^product/(.*) /prods.php?id=${product2id:$1|NOTFOUND} [PT] +
-RewriteMap real-to-user txt:/path/to/file/map.txt -
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 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.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 txt
is 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
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:
+ +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:
## -## 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
.
.htaccess
+ file, you'll need to remove the leading slash from the rewrite
+ pattern in order for it to match anything: