<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
+<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" />
+<!--
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
This file is generated from xml source: DO NOT EDIT
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-->
-<title>Using RewriteMap - Apache HTTP Server</title>
+<title>Using RewriteMap - Apache HTTP Server Version 2.5</title>
<link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
<link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
-<link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
+<link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" />
+<script src="../style/scripts/prettify.min.js" type="text/javascript">
+</script>
+
<link href="../images/favicon.ico" rel="shortcut icon" /></head>
<body id="manual-page"><div id="page-header">
-<p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
-<p class="apache">Apache HTTP Server Version 2.3</p>
+<p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
+<p class="apache">Apache HTTP Server Version 2.5</p>
<img alt="" src="../images/feather.gif" /></div>
<div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
<div id="path">
-<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.3</a> > <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Using RewriteMap</h1>
+<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.5</a> > <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Using RewriteMap</h1>
<div class="toplang">
<p><span>Available Languages: </span><a href="../en/rewrite/rewritemap.html" title="English"> en </a></p>
</div>
- <p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
+ <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>
</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>
-</ul><h3>See also</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Module documentation</a></li><li><a href="intro.html">mod_rewrite introduction</a></li><li><a href="remapping.html">Redirection and remapping</a></li><li><a href="access.html">Controlling access</a></li><li><a href="vhosts.html">Virtual hosts</a></li><li><a href="proxy.html">Proxying</a></li><li><a href="advanced.html">Advanced techniques and tricks</a></li><li><a href="avoid.html">When not to use mod_rewrite</a></li></ul></div>
+</ul><h3>See also</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Module documentation</a></li><li><a href="intro.html">mod_rewrite introduction</a></li><li><a href="remapping.html">Redirection and remapping</a></li><li><a href="access.html">Controlling access</a></li><li><a href="vhosts.html">Virtual hosts</a></li><li><a href="proxy.html">Proxying</a></li><li><a href="advanced.html">Advanced techniques</a></li><li><a href="avoid.html">When not to use mod_rewrite</a></li></ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
<div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="introduction" id="introduction">Introduction</a></h2>
<p>The syntax of the <code>RewriteMap</code> directive is as
follows:</p>
-<div class="example"><p><code>
-RewriteMap <em>MapName</em> <em>MapType</em>:<em>MapSource</em>
-</code></p></div>
-
+<pre class="prettyprint lang-config">RewriteMap <em>MapName</em> <em>MapType</em>:<em>MapSource</em>
+</pre>
+
+
<p>The <a id="mapfunc" name="mapfunc"><em>MapName</em></a> is an
arbitray name that you assign to the map, and which you will use in
directives later on. Arguments are passed to the map via the
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
+ <p>For example, you can define a
<code class="directive">RewriteMap</code> as:</p>
- <div class="example"><p><code>
- RewriteMap examplemap txt:/path/to/file/map.txt
- </code></p></div>
+ <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>
-<div class="example"><p><code>
- RewriteRule ^/ex/(.*) ${examplemap:$1}
-</code></p></div>
+ <pre class="prettyprint lang-config">RewriteRule "^/ex/(.*)" "${examplemap:$1}"</pre>
+
<p>A default value can be specified in the event that nothing is found
in the map:</p>
-<div class="example"><p><code>
-RewriteRule ^/ex/(.*) ${examplemap:$1|/not_found.html}
-</code></p></div>
+<pre class="prettyprint lang-config">RewriteRule "^/ex/(.*)" "${examplemap:$1|/not_found.html}"</pre>
+
<div class="note"><h3>Per-directory and .htaccess context</h3>
<p>
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 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 "(.*?[A-Z]+.*)" "${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>
per line. Optionally, a line may be 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
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>
+ <pre class="prettyprint lang-config">RewriteMap product2id "txt:/etc/apache2/productmap.txt"
+RewriteRule "^/product/(.*)" "/prods.php?id=${product2id:$1|NOTFOUND}" [PT]</pre>
- <div class="example"><h3>Product to ID configuration</h3><p><code>
- RewriteMap product2id txt:/etc/apache2/productmap.txt<br />
- RewriteRule ^/product/(.*) /prods.php?id=${product2id:$1|NOTFOUND} [PT]
- </code></p></div>
<p>We assume here that the <code>prods.php</code> script knows what
to do when it received an argument of <code>id=NOTFOUND</code> when
scope. If you're planning to use this in a <code>.htaccess</code>
file, you'll need to remove the leading slash from the rewrite
pattern in order for it to match anything:
- <div class="example"><p><code>
- RewriteRule ^product/(.*) /prods.php?id=${product2id:$1|NOTFOUND} [PT]
- </code></p></div>
+ <pre class="prettyprint lang-config">RewriteRule "^product/(.*)" "/prods.php?id=${product2id:$1|NOTFOUND}" [PT]</pre>
+
</div>
<div class="note"><h3>Cached lookups</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 server, via a reverse-proxy. Images are sent
+ 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.</p>
static www1|www2|www3|www4<br />
dynamic www5|www6
</code></p></div>
+<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>
- <div class="example"><h3>Configuration directives</h3><p><code>
- RewriteMap servers rnd:/path/to/file/map.txt<br />
- <br />
- RewriteRule ^/(.*\.(png|gif|jpg)) http://${servers:static}/$1 [NC,P,L]<br />
- RewriteRule ^/(.*) http://${servers:dynamic}/$1 [P,L]
- </code></p></div>
<p>So, when an image is requested and the first of these rules is
matched, <code>RewriteMap</code> looks up the string
<p>You may optionally specify a particular dbm type:</p>
- <div class="example"><p><code>
- RewriteMap examplemap dbm=sdbm:/etc/apache/mapfile.dbm
- </code></p></div>
+ <pre class="prettyprint lang-config">RewriteMap examplemap "dbm=sdbm:/etc/apache/mapfile.dbm"</pre>
+
<p>The type can be sdbm, gdbm, ndbm or db.
However, it is recommended that you just use the <a href="../programs/httxt2dbm.html">httxt2dbm</a> utility that is
<p>You can then reference the resulting file in your
<code>RewriteMap</code> directive:</p>
-<div class="example"><p><code>
-RewriteMap mapname dbm:/etc/apache/mapfile.map
-</code></p></div>
+<pre class="prettyprint lang-config">RewriteMap mapname "dbm:/etc/apache/mapfile.map"</pre>
+
<div class="note">
<p>Note that with some dbm types, more than one file is generated, with
</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>
-
- <div class="example"><h3>Redirect a URI to an all-lowercase version of itself</h3><p><code>
- RewriteMap lc int:tolower<br />
- RewriteRule (.*?[A-Z]+.*) ${lc:$1} [R]
- </code></p></div>
-
- <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
a context that does not have <code class="directive"><a href="../mod/mod_rewrite.html#rewriteengine">RewriteEngine</a></code> set to
<code>on</code>.</p>
+ <p>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
+ <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> in the
+ <code>username:groupname</code> format.</p>
+
<p>This feature utilizes the <code>rewrite-map</code> mutex,
which is required for reliable communication with the program.
The mutex mechanism and lock file can be configured with the
<p>A simple example is shown here which will replace all dashes with
underscores in a request URI.</p>
- <div class="example"><h3>Rewrite configuration</h3><p><code>
- RewriteMap d2u prg:/www/bin/dash2under.pl<br />
- RewriteRule - ${d2u:%{REQUEST_URI}}
- </code></p></div>
+<p><strong>Rewrite configuration</strong></p>
+ <pre class="prettyprint lang-config">RewriteMap d2u "prg:/www/bin/dash2under.programlisting" apache:apache<br />
+RewriteRule "-" "${d2u:%{REQUEST_URI}}"</pre>
+
+
+ <p><strong>dash2under.pl</strong></p>
+ <pre class="prettyprint lang-perl"> #!/usr/bin/perl
+ $| = 1; # Turn off I/O buffering
+ while (<STDIN>) {
+ s/-/_/g; # Replace dashes with underscores
+ print $_;
+ }</pre>
- <div class="example"><h3>dash2under.pl</h3><p><code>
- #!/usr/bin/perl<br />
- $| = 1; # Turn off I/O buffering<br />
- while (<STDIN>) {<br />
- <span class="indent">
- s/-/_/g; # Replace dashes with underscores<br />
- print $_;<br />
- </span>
- }<br />
- </code></p></div>
<div class="note"><h3>Caution!</h3>
<ul>
<p>If a query returns more than one row, a random row from
the result set is used.</p>
- <div class="example"><h3>Example</h3><p><code>
-RewriteMap myquery "fastdbd:SELECT destination FROM rewrite WHERE source = %s"
- </code></p></div>
+ <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>
<div class="section">
once. For each mapping-function use one
<code class="directive">RewriteMap</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
</div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="../en/rewrite/rewritemap.html" title="English"> en </a></p>
-</div><div id="footer">
-<p class="apache">Copyright 2011 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
-<p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div>
+</div><div class="top"><a href="#page-header"><img src="../images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
+<script type="text/javascript"><!--//--><![CDATA[//><!--
+var comments_shortname = 'httpd';
+var comments_identifier = 'http://httpd.apache.org/docs/trunk/rewrite/rewritemap.html';
+(function(w, d) {
+ if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
+ d.write('<div id="comments_thread"><\/div>');
+ var s = d.createElement('script');
+ s.type = 'text/javascript';
+ s.async = true;
+ s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
+ (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
+ }
+ else {
+ d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
+ }
+})(window, document);
+//--><!]]></script></div><div id="footer">
+<p class="apache">Copyright 2015 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
+<p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
+if (typeof(prettyPrint) !== 'undefined') {
+ prettyPrint();
+}
+//--><!]]></script>
</body></html>
\ No newline at end of file