advanced.html.en revision 205f749042ed530040a4f0080dbcb47ceae8a374
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
a78048ccbdb6256da15e6b0e7e95355e480c2301nd XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
a78048ccbdb6256da15e6b0e7e95355e480c2301nd This file is generated from xml source: DO NOT EDIT
a78048ccbdb6256da15e6b0e7e95355e480c2301nd XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<title>Advanced Techniques with mod_rewrite - Apache HTTP Server Version 2.5</title>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<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" />
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen<script src="/style/scripts/prettify.min.js" type="text/javascript">
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen<link href="/images/favicon.ico" rel="shortcut icon" /></head>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<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>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<div class="up"><a href="./"><img title="<-" alt="<-" src="/images/left.gif" /></a></div>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<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>Advanced Techniques with mod_rewrite</h1>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<p><span>Available Languages: </span><a href="/en/rewrite/advanced.html" title="English"> en </a></p>
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung<p>This document supplements the <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<a href="/mod/mod_rewrite.html">reference documentation</a>. It provides
4b575a6b6704b516f22d65a3ad35696d7b9ba372rpluema few advanced techniques using mod_rewrite.</p>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<div class="warning">Note that many of these examples won't work unchanged in your
a78048ccbdb6256da15e6b0e7e95355e480c2301ndparticular server configuration, so it's important that you understand
a78048ccbdb6256da15e6b0e7e95355e480c2301ndthem, rather than merely cutting and pasting the examples into your
a78048ccbdb6256da15e6b0e7e95355e480c2301ndconfiguration.</div>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#sharding">URL-based sharding across multiple backends</a></li>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<li><img alt="" src="/images/down.gif" /> <a href="#on-the-fly-content">On-the-fly Content-Regeneration</a></li>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<li><img alt="" src="/images/down.gif" /> <a href="#load-balancing">Load Balancing</a></li>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<li><img alt="" src="/images/down.gif" /> <a href="#structuredhomedirs">Structured Userdirs</a></li>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<li><img alt="" src="/images/down.gif" /> <a href="#redirectanchors">Redirecting Anchors</a></li>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<li><img alt="" src="/images/down.gif" /> <a href="#time-dependent">Time-Dependent Rewriting</a></li>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<li><img alt="" src="/images/down.gif" /> <a href="#setenvvars">Set Environment Variables Based On URL Parts</a></li>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd</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="rewritemap.html">Using RewriteMap</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>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<h2><a name="sharding" id="sharding">URL-based sharding across multiple backends</a></h2>
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak <p>A common technique for distributing the burden of
a78048ccbdb6256da15e6b0e7e95355e480c2301nd server load or storage space is called "sharding".
a78048ccbdb6256da15e6b0e7e95355e480c2301nd When using this method, a front-end server will use the
a78048ccbdb6256da15e6b0e7e95355e480c2301nd url to consistently "shard" users or objects to separate
a78048ccbdb6256da15e6b0e7e95355e480c2301nd backend servers.</p>
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak <p>A mapping is maintained, from users to target servers, in
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak external map files. They look like:</p>
a78048ccbdb6256da15e6b0e7e95355e480c2301nduser1 physical_host_of_user1<br />
a78048ccbdb6256da15e6b0e7e95355e480c2301nduser2 physical_host_of_user2<br />
a78048ccbdb6256da15e6b0e7e95355e480c2301nd <p>We put this into a <code>map.users-to-hosts</code> file. The
a78048ccbdb6256da15e6b0e7e95355e480c2301nd aim is to map;</p>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd <p>thus every URL path need not be valid on every backend physical
a78048ccbdb6256da15e6b0e7e95355e480c2301nd host. The following ruleset does this for us with the help of the map
a78048ccbdb6256da15e6b0e7e95355e480c2301nd files assuming that server0 is a default server which will be used if
a78048ccbdb6256da15e6b0e7e95355e480c2301nd a user has no entry in the map:</p>
a78048ccbdb6256da15e6b0e7e95355e480c2301ndRewriteRule ^/u/([^/]+)/?(.*) http://${users-to-hosts:$1|server0}/u/$1/$2</pre>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd <p>See the <code class="directive"><a href="/mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd documentation for more discussion of the syntax of this directive.</p>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<h2><a name="on-the-fly-content" id="on-the-fly-content">On-the-fly Content-Regeneration</a></h2>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd <p>We wish to dynamically generate content, but store it
a78048ccbdb6256da15e6b0e7e95355e480c2301nd statically once it is generated. This rule will check for the
a78048ccbdb6256da15e6b0e7e95355e480c2301nd existence of the static file, and if it's not there, generate
a78048ccbdb6256da15e6b0e7e95355e480c2301nd it. The static files can be removed periodically, if desired (say,
a78048ccbdb6256da15e6b0e7e95355e480c2301nd via cron) and will be regenerated on demand.</p>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd This is done via the following ruleset:
a78048ccbdb6256da15e6b0e7e95355e480c2301nd<pre class="prettyprint lang-config"># This example is valid in per-directory context only
a78048ccbdb6256da15e6b0e7e95355e480c2301ndRewriteCond %{REQUEST_URI} !-U
a78048ccbdb6256da15e6b0e7e95355e480c2301ndRewriteRule ^(.+)\.html$ /regenerate_page.cgi [PT,L]</pre>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd <p>The <code>-U</code> operator determines whether the test string
a78048ccbdb6256da15e6b0e7e95355e480c2301nd (in this case, <code>REQUEST_URI</code>) is a valid URL. It does
a78048ccbdb6256da15e6b0e7e95355e480c2301nd this via a subrequest. In the event that this subrequest fails -
a78048ccbdb6256da15e6b0e7e95355e480c2301nd that is, the requested resource doesn't exist - this rule invokes
a78048ccbdb6256da15e6b0e7e95355e480c2301nd the CGI program <code>/regenerate_page.cgi</code>, which generates
a78048ccbdb6256da15e6b0e7e95355e480c2301nd the requested resource and saves it into the document directory, so
a78048ccbdb6256da15e6b0e7e95355e480c2301nd that the next time it is requested, a static copy can be served.</p>
a78048ccbdb6256da15e6b0e7e95355e480c2301nd <p>In this way, documents that are infrequently updated can be served in
a78048ccbdb6256da15e6b0e7e95355e480c2301nd static form. if documents need to be refreshed, they can be deleted
a78048ccbdb6256da15e6b0e7e95355e480c2301nd from the document directory, and they will then be regenerated the
a78048ccbdb6256da15e6b0e7e95355e480c2301nd next time they are requested.</p>
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh<h2><a name="load-balancing" id="load-balancing">Load Balancing</a></h2>
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh <p>We wish to randomly distribute load across several servers
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh using mod_rewrite.</p>
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh <p>We'll use <code class="directive"><a href="/mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> and a list of servers
5effc8b39fae5cd169d17f342bfc265705840014rbowen to accomplish this.</p>
7fec19672a491661b2fe4b29f685bc7f4efa64d4ndRewriteRule ^/(.*) http://${lb:servers}/$1 [P,L]</pre>
<code class="module"><a href="/mod/mod_proxy_balancer.html">mod_proxy_balancer</a></code> - which is far more flexible and
RewriteRule ^/~(<strong>([a-z])</strong>[a-z0-9]+)(.*) /home/<strong>$2</strong>/$1/public_html$3</pre>
RewriteRule ^foo\.html$ foo.day.html [L]
<div class="warning"><code class="module"><a href="/mod/mod_cache.html">mod_cache</a></code>, intermediate proxies
<code class="module"><a href="/mod/mod_expires.html">mod_expires</a></code> may be used to control this
<p><span>Available Languages: </span><a href="/en/rewrite/advanced.html" title="English"> en </a></p>
</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>
var comments_identifier = 'http://httpd.apache.org/docs/trunk/rewrite/advanced.html';
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);
d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
<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[//><!--