<p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
<a href="../mod/mod_rewrite.html">reference documentation</a>. It describes
the use of the <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> directive,
-and provides examples of each of the various <code>RewriteMap</code> types.</p>
+and provides examples of each of the various <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> types.</p>
<div class="warning">Note that many of these examples won't work unchanged in your
particular server configuration, so it's important that you understand
</div>
<div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#introduction">Introduction</a></li>
+<li><img alt="" src="../images/down.gif" /> <a href="#int">int: Internal Function</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#txt">txt: Plain text maps</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#rnd">rnd: Randomized Plain Text</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#dbm">dbm: DBM Hash File</a></li>
-<li><img alt="" src="../images/down.gif" /> <a href="#int">int: Internal Function</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#prg">prg: External Rewriting Program</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#dbd">dbd or fastdbd: SQL Query</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#summary">Summary</a></li>
the <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> reference
documentation.</p>
- <p>The syntax of the <code>RewriteMap</code> directive is as
- follows:</p>
+ <p>The syntax of the <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
+ directive is as follows:</p>
<pre class="prettyprint lang-config">RewriteMap <em>MapName</em> <em>MapType</em>:<em>MapSource</em>
</pre>
substituted by <em>DefaultValue</em> or by the empty string
if no <em>DefaultValue</em> was specified.</p>
- <p>For example, you might define a
- <code class="directive">RewriteMap</code> as:</p>
+ <p>For example, you can define a
+ <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> as:</p>
<pre class="prettyprint lang-config">RewriteMap examplemap "txt:/path/to/file/map.txt"</pre>
<p>You would then be able to use this map in a
- <code class="directive">RewriteRule</code> as follows:</p>
-<pre class="prettyprint lang-config">RewriteRule "^/ex/(.*)" "${examplemap:$1}"</pre>
+ <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> as follows:</p>
+ <pre class="prettyprint lang-config">RewriteRule "^/ex/(.*)" "${examplemap:$1}"</pre>
<p>A default value can be specified in the event that nothing is found
<div class="note"><h3>Per-directory and .htaccess context</h3>
<p>
-The <code>RewriteMap</code> directive may not be used in
-<Directory> sections or <code>.htaccess</code> files. You must
+The <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> directive may not be
+used in <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code> sections or
+<code>.htaccess</code> files. You must
declare the map in server or virtualhost context. You may use the map,
-once created, in your <code>RewriteRule</code> and
-<code>RewriteCond</code> directives in those scopes. You just can't
-<strong>declare</strong> it in those scopes.
-</p>
+once created, in your <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> and
+<code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> directives in those
+scopes. You just can't <strong>declare</strong> it in those scopes.</p>
</div>
<p>The sections that follow describe the various <em>MapType</em>s that
may be used, and give examples of each.</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
+<h2><a name="int" id="int">int: Internal Function</a></h2>
+
+
+ <p>When a MapType of <code>int</code> is used, the MapSource is one
+ of the available internal <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
+ functions. Module authors can provide
+ additional internal functions by registering them with the
+ <code>ap_register_rewrite_mapfunc</code> API.
+ The functions that are provided by default are:
+ </p>
+
+ <ul>
+ <li><strong>toupper</strong>:<br />
+ Converts the key to all upper case.</li>
+ <li><strong>tolower</strong>:<br />
+ Converts the key to all lower case.</li>
+ <li><strong>escape</strong>:<br />
+ Translates special characters in the key to
+ hex-encodings.</li>
+ <li><strong>unescape</strong>:<br />
+ Translates hex-encodings in the key back to
+ special characters.</li>
+ </ul>
+
+ <p>
+ To use one of these functions, create a <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> referencing
+ the int function, and then use that in your <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>:
+ </p>
+
+ <p> <strong>Redirect a URI to an all-lowercase version of itself</strong></p>
+ <pre class="prettyprint lang-config">RewriteMap lc int:tolower
+RewriteRule "(.*)" "${lc:$1}" [R]</pre>
+
+
+ <div class="note">
+ <p>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
+ <code class="module"><a href="../mod/mod_speling.html">mod_speling</a></code> instead.
+ </p>
+ </div>
+
+ </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
+<div class="section">
<h2><a name="txt" id="txt">txt: Plain text maps</a></h2>
<p>When a MapType of <code>txt</code> 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
+ plain-text mapping file, containing one space-separated key/value pair
+ per line. Optionally, a line may contain a comment, starting with
a '#' character.</p>
- <p>For example, the following might be valid entries in a map
- file.</p>
+ <p>A valid text rewrite map file will have the following syntax:</p>
- <p class="indent">
+ <div class="example"><p><code>
# Comment line<br />
<strong><em>MatchingKey</em> <em>SubstValue</em></strong><br />
<strong><em>MatchingKey</em> <em>SubstValue</em></strong> # comment<br />
- </p>
+ </code></p></div>
- <p>When the RewriteMap is invoked the argument is looked for in the
+ <p>When the <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> is invoked
+ the argument is looked for in the
first argument of a line, and, if found, the substitution value is
returned.</p>
- <p>For example, we might use a mapfile to translate product names to
+ <p>For example, we can use a mapfile to translate product names to
product IDs for easier-to-remember URLs, using the following
recipe:</p>
<p><strong>Product to ID configuration</strong></p>
</code></p></div>
<p>Thus, when <code>http://example.com/product/television</code> is
- requested, the <code>RewriteRule</code> is applied, and the request
+ requested, the <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> is
+ applied, and the request
is internally mapped to <code>/prods.php?id=993</code>.</p>
<div class="note"><h3>Note: .htaccess files</h3>
One of these values will be chosen at random if the key is
matched.</p>
- <p>For example, you might use the following map
+ <p>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
<p><strong>Configuration directives</strong></p>
<pre class="prettyprint lang-config">RewriteMap servers "rnd:/path/to/file/map.txt"
-RewriteRule "^/(.*\.(png|gif|jpg))" "http://${servers:static}/$1" [NC,P,L]
-RewriteRule "^/(.*)" "http://${servers:dynamic}/$1" [P,L]</pre>
+RewriteRule "^/(.*\.(png|gif|jpg))" "http://${servers:static}/$1" [NC,P,L]
+RewriteRule "^/(.*)" "http://${servers:dynamic}/$1" [P,L]</pre>
<p>So, when an image is requested and the first of these rules is
- matched, <code>RewriteMap</code> looks up the string
+ matched, <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> looks up the string
<code>static</code> in the map file, which returns one of the
specified hostnames at random, which is then used in the
- <code>RewriteRule</code> target.</p>
+ <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> target.</p>
<p>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,
<pre class="prettyprint lang-config">RewriteMap examplemap "dbm=sdbm:/etc/apache/mapfile.dbm"</pre>
- <p>The type can be sdbm, gdbm, ndbm or db.
+ <p>The type can be <code>sdbm</code>, <code>gdbm</code>, <code>ndbm</code>
+ or <code>db</code>.
However, it is recommended that you just use the <a href="../programs/httxt2dbm.html">httxt2dbm</a> 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.</p>
</code></p></div>
<p>You can then reference the resulting file in your
-<code>RewriteMap</code> directive:</p>
+<code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> directive:</p>
<pre class="prettyprint lang-config">RewriteMap mapname "dbm:/etc/apache/mapfile.map"</pre>
a common base name. For example, you may have two files named
<code>mapfile.map.dir</code> and <code>mapfiile.map.pag</code>. This is
normal, and you need only use the base name <code>mapfile.map</code> in
-your <code>RewriteMap</code> directive.</p>
+your <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> directive.</p>
</div>
<div class="note"><h3>Cached lookups</h3>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
-<h2><a name="int" id="int">int: Internal Function</a></h2>
-
-
- <p>When a MapType of <code>int</code> is used, the MapSource is one
- of the available internal RewriteMap functions. Module authors can provide
- additional internal functions by registering them with the
- <code>ap_register_rewrite_mapfunc</code> API.
- The functions that are provided by default are:
- </p>
-
- <ul>
- <li><strong>toupper</strong>:<br />
- Converts the key to all upper case.</li>
- <li><strong>tolower</strong>:<br />
- Converts the key to all lower case.</li>
- <li><strong>escape</strong>:<br />
- Translates special characters in the key to
- hex-encodings.</li>
- <li><strong>unescape</strong>:<br />
- Translates hex-encodings in the key back to
- special characters.</li>
- </ul>
-
- <p>
- To use one of these functions, create a <code>RewriteMap</code> referencing
- the int function, and then use that in your <code>RewriteRule</code>:
- </p>
-
- <p> <strong>Redirect a URI to an all-lowercase version of itself</strong></p>
- <pre class="prettyprint lang-config">RewriteMap lc int:tolower
-RewriteRule "(.*)" "${lc:$1}" [R]</pre>
-
-
- <div class="note">
- <p>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
- <code class="module"><a href="../mod/mod_speling.html">mod_speling</a></code> instead.
- </p>
- </div>
-
- </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
-<div class="section">
<h2><a name="prg" id="prg">prg: External Rewriting Program</a></h2>
<p>When a MapType of <code>prg</code> is used, the MapSource is a
<p>A simple example is shown here which will replace all dashes with
underscores in a request URI.</p>
-
-<p><strong>Rewrite configuration</strong></p>
+
+ <p><strong>Rewrite configuration</strong></p>
<pre class="prettyprint lang-config">RewriteMap d2u "prg:/www/bin/dash2under.pl"<br />
RewriteRule "-" "${d2u:%{REQUEST_URI}}"</pre>
restarted.</p>
<p>If a query returns more than one row, a random row from
-the result set is used.</p>
+ the result set is used.</p>
- <div class="example"><h3>Example</h3><pre class="prettyprint lang-config">RewriteMap myquery "fastdbd:SELECT destination FROM rewrite WHERE source = %s"</pre>
+ <div class="example"><h3>Example</h3><pre class="prettyprint lang-config">RewriteMap myquery "fastdbd:SELECT destination FROM rewrite WHERE source = %s"</pre>
</div>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<h2><a name="summary" id="summary">Summary</a></h2>
- <p>The <code class="directive">RewriteMap</code> directive can occur more than
- once. For each mapping-function use one
- <code class="directive">RewriteMap</code> directive to declare its rewriting
- mapfile.</p>
+ <p>The <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> directive can
+ occur more than once. For each mapping-function use one
+ <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> directive to declare
+ its rewriting mapfile.</p>
<p>While you cannot <strong>declare</strong> a map in
per-directory context (<code>.htaccess</code> files or
- <Directory> blocks) it is possible to
- <strong>use</strong> this map in per-directory context. </p>
+ <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code> blocks) it is
+ possible to <strong>use</strong> this map in per-directory context.</p>
</div></div>
<div class="bottomlang">