advanced.html.en revision 4aa603e6448b99f9371397d439795c91a93637ea
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<?xml version="1.0" encoding="ISO-8859-1"?>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen This file is generated from xml source: DO NOT EDIT
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen -->
149dc5253eef0f0289fad126961eec1c9e1407a2rbowen<title>Advanced Techniques with mod_rewrite - Apache HTTP Server</title>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen<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" />
2e545ce2450a9953665f701bb05350f0d3f26275nd<script src="/style/scripts/prettify.min.js" type="text/javascript">
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen</script>
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<link href="/images/favicon.ico" rel="shortcut icon" /></head>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<body id="manual-page"><div id="page-header">
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen<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>
3f08db06526d6901aa08c110b5bc7dde6bc39905nd<p class="apache">Apache HTTP Server Version 2.5</p>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<img alt="" src="/images/feather.gif" /></div>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="/images/left.gif" /></a></div>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<div id="path">
3f08db06526d6901aa08c110b5bc7dde6bc39905nd<a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.5</a> &gt; <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Advanced Techniques with mod_rewrite</h1>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<div class="toplang">
b6e6d2139d50d64fc4bbd01c4f07d7a4accfec8cnd<p><span>Available Languages: </span><a href="/en/rewrite/advanced.html" title="English">&nbsp;en&nbsp;</a></p>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen</div>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf<p>This document supplements the <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf<a href="/mod/mod_rewrite.html">reference documentation</a>. It provides
53abc235688d883cfa15cdfec354ba03128f357arbowena few advanced techniques using mod_rewrite.</p>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<div class="warning">Note that many of these examples won't work unchanged in your
b630661b71a5495a95831a05a6a2299fb1c7c771rbowenparticular server configuration, so it's important that you understand
b630661b71a5495a95831a05a6a2299fb1c7c771rbowenthem, rather than merely cutting and pasting the examples into your
b630661b71a5495a95831a05a6a2299fb1c7c771rbowenconfiguration.</div>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen</div>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#sharding">URL-based sharding accross multiple backends</a></li>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen<li><img alt="" src="/images/down.gif" /> <a href="#on-the-fly-content">On-the-fly Content-Regeneration</a></li>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen<li><img alt="" src="/images/down.gif" /> <a href="#load-balancing">Load Balancing</a></li>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen<li><img alt="" src="/images/down.gif" /> <a href="#autorefresh">Document With Autorefresh</a></li>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen<li><img alt="" src="/images/down.gif" /> <a href="#structuredhomedirs">Structured Userdirs</a></li>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen<li><img alt="" src="/images/down.gif" /> <a href="#redirectanchors">Redirecting Anchors</a></li>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen<li><img alt="" src="/images/down.gif" /> <a href="#time-dependent">Time-Dependent Rewriting</a></li>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen<li><img alt="" src="/images/down.gif" /> <a href="#setenvvars">Set Environment Variables Based On URL Parts</a></li>
3cfb051391f3d6aa28a25ca3feb81e841242f9d5humbedooh</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>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen<div class="section">
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen<h2><a name="sharding" id="sharding">URL-based sharding accross multiple backends</a></h2>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen <dl>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen <dt>Description:</dt>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen <dd>
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf <p>A common technique for distributing the burden of
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf server load or storage space is called "sharding".
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen When using this method, a front-end server will use the
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen url to consistently "shard" users or objects to separate
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen backend servers.</p>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen </dd>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen <dt>Solution:</dt>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen <dd>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen <p>A mapping is maintained, from users to target servers, in
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen external map files. They look like:</p>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
353e838f5716b394f2f9bf38549e1550a5e73570rbowen<div class="example"><p><code>
353e838f5716b394f2f9bf38549e1550a5e73570rbowenuser1 physical_host_of_user1<br />
353e838f5716b394f2f9bf38549e1550a5e73570rbowenuser2 physical_host_of_user2<br />
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen: :
353e838f5716b394f2f9bf38549e1550a5e73570rbowen</code></p></div>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen <p>We put this into a <code>map.users-to-hosts</code> file. The
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen aim is to map;</p>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
353e838f5716b394f2f9bf38549e1550a5e73570rbowen<div class="example"><p><code>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen/u/user1/anypath
353e838f5716b394f2f9bf38549e1550a5e73570rbowen</code></p></div>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen <p>to</p>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
353e838f5716b394f2f9bf38549e1550a5e73570rbowen<div class="example"><p><code>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowenhttp://physical_host_of_user1/u/user/anypath
353e838f5716b394f2f9bf38549e1550a5e73570rbowen</code></p></div>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen <p>thus every URL path need not be valid on every backend physical
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen host. The following ruleset does this for us with the help of the map
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen files assuming that server0 is a default server which will be used if
c2a88a7ef740c408aba42af38ddfb9ab8a7398d9nd a user has no entry in the map:</p>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
4aa603e6448b99f9371397d439795c91a93637eand<pre class="prettyprint lang-config">RewriteEngine on
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedoohRewriteMap users-to-hosts txt:/path/to/map.users-to-hosts
4aa603e6448b99f9371397d439795c91a93637eandRewriteRule ^/u/([^/]+)/?(.*) http://${users-to-hosts:$1|server0}/u/$1/$2</pre>
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedooh
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen </dd>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen </dl>
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen
53abc235688d883cfa15cdfec354ba03128f357arbowen <p>See the <code class="directive"><a href="/mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
53abc235688d883cfa15cdfec354ba03128f357arbowen documentation for more discussion of the syntax of this directive.</p>
53abc235688d883cfa15cdfec354ba03128f357arbowen
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen<div class="section">
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen<h2><a name="on-the-fly-content" id="on-the-fly-content">On-the-fly Content-Regeneration</a></h2>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen <dl>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen <dt>Description:</dt>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen <dd>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen <p>We wish to dynamically generate content, but store it
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen statically once it is generated. This rule will check for the
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen existence of the static file, and if it's not there, generate
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen it. The static files can be removed periodically, if desired (say,
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen via cron) and will be regenerated on demand.</p>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen </dd>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen <dt>Solution:</dt>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen <dd>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen This is done via the following ruleset:
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen
4aa603e6448b99f9371397d439795c91a93637eand<pre class="prettyprint lang-config"># This example is valid in per-directory context only
04a56f9892b2481f1d10336848cc3a140780cf20humbedoohRewriteCond %{REQUEST_URI} !-U
4aa603e6448b99f9371397d439795c91a93637eandRewriteRule ^(.+)\.html$ /regenerate_page.cgi [PT,L]</pre>
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedooh
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen
53abc235688d883cfa15cdfec354ba03128f357arbowen <p>The <code>-U</code> operator determines whether the test string
53abc235688d883cfa15cdfec354ba03128f357arbowen (in this case, <code>REQUEST_URI</code>) is a valid URL. It does
53abc235688d883cfa15cdfec354ba03128f357arbowen this via a subrequest. In the event that this subrequest fails -
53abc235688d883cfa15cdfec354ba03128f357arbowen that is, the requested resource doesn't exist - this rule invokes
53abc235688d883cfa15cdfec354ba03128f357arbowen the CGI program <code>/regenerate_page.cgi</code>, which generates
53abc235688d883cfa15cdfec354ba03128f357arbowen the requested resource and saves it into the document directory, so
53abc235688d883cfa15cdfec354ba03128f357arbowen that the next time it is requested, a static copy can be served.</p>
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf
53abc235688d883cfa15cdfec354ba03128f357arbowen <p>In this way, documents that are infrequently updated can be served in
53abc235688d883cfa15cdfec354ba03128f357arbowen static form. if documents need to be refreshed, they can be deleted
53abc235688d883cfa15cdfec354ba03128f357arbowen from the document directory, and they will then be regenerated the
53abc235688d883cfa15cdfec354ba03128f357arbowen next time they are requested.</p>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen </dd>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen </dl>
b0306ceedec510a7b4ebe3a1b70c366bf5f2727crbowen
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen<div class="section">
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen<h2><a name="load-balancing" id="load-balancing">Load Balancing</a></h2>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen <dl>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen <dt>Description:</dt>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen <dd>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen <p>We wish to randomly distribute load across several servers
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen using mod_rewrite.</p>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen </dd>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen <dt>Solution:</dt>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen <dd>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen <p>We'll use <code class="directive"><a href="/mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> and a list of servers
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen to accomplish this.</p>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
4aa603e6448b99f9371397d439795c91a93637eand<pre class="prettyprint lang-config">RewriteEngine on
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedoohRewriteMap lb rnd:/path/to/serverlist.txt
4aa603e6448b99f9371397d439795c91a93637eandRewriteRule ^/(.*) http://${lb:servers}/$1 [P,L]</pre>
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedooh
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen<p><code>serverlist.txt</code> will contain a list of the servers:</p>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
353e838f5716b394f2f9bf38549e1550a5e73570rbowen<div class="example"><p><code>
353e838f5716b394f2f9bf38549e1550a5e73570rbowen## serverlist.txt<br />
353e838f5716b394f2f9bf38549e1550a5e73570rbowen<br />
353e838f5716b394f2f9bf38549e1550a5e73570rbowenservers one.example.com|two.example.com|three.example.com<br />
353e838f5716b394f2f9bf38549e1550a5e73570rbowen</code></p></div>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen<p>If you want one particular server to get more of the load than the
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowenothers, add it more times to the list.</p>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen </dd>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen <dt>Discussion</dt>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen <dd>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen<p>Apache comes with a load-balancing module -
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen<code class="module"><a href="/mod/mod_proxy_balancer.html">mod_proxy_balancer</a></code> - which is far more flexible and
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowenfeatureful than anything you can cobble together using mod_rewrite.</p>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen </dd>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen </dl>
ed84b2ae96180924248fbbc86b68d86ae8d88cb3rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen<div class="section">
0f6366523dec7face01fcad3eadcc1378e34e663rbowen<h2><a name="autorefresh" id="autorefresh">Document With Autorefresh</a></h2>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
53abc235688d883cfa15cdfec354ba03128f357arbowen
53abc235688d883cfa15cdfec354ba03128f357arbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen <dl>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen <dt>Description:</dt>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen <dd>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen <p>Wouldn't it be nice, while creating a complex web page, if
0f6366523dec7face01fcad3eadcc1378e34e663rbowen the web browser would automatically refresh the page every
0f6366523dec7face01fcad3eadcc1378e34e663rbowen time we save a new version from within our editor?
0f6366523dec7face01fcad3eadcc1378e34e663rbowen Impossible?</p>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen </dd>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen <dt>Solution:</dt>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen <dd>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen <p>No! We just combine the MIME multipart feature, the
0f6366523dec7face01fcad3eadcc1378e34e663rbowen web server NPH feature, and the URL manipulation power of
0f6366523dec7face01fcad3eadcc1378e34e663rbowen <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>. First, we establish a new
0f6366523dec7face01fcad3eadcc1378e34e663rbowen URL feature: Adding just <code>:refresh</code> to any
0f6366523dec7face01fcad3eadcc1378e34e663rbowen URL causes the 'page' to be refreshed every time it is
0f6366523dec7face01fcad3eadcc1378e34e663rbowen updated on the filesystem.</p>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
4aa603e6448b99f9371397d439795c91a93637eand<pre class="prettyprint lang-config">RewriteRule ^(/[uge]/[^/]+/?.*):refresh /internal/cgi/apache/nph-refresh?f=$1</pre>
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedooh
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen <p>Now when we reference the URL</p>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
353e838f5716b394f2f9bf38549e1550a5e73570rbowen<div class="example"><p><code>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen/u/foo/bar/page.html:refresh
353e838f5716b394f2f9bf38549e1550a5e73570rbowen</code></p></div>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen <p>this leads to the internal invocation of the URL</p>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
353e838f5716b394f2f9bf38549e1550a5e73570rbowen<div class="example"><p><code>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen/internal/cgi/apache/nph-refresh?f=/u/foo/bar/page.html
353e838f5716b394f2f9bf38549e1550a5e73570rbowen</code></p></div>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen <p>The only missing part is the NPH-CGI script. Although
0f6366523dec7face01fcad3eadcc1378e34e663rbowen one would usually say "left as an exercise to the reader"
0f6366523dec7face01fcad3eadcc1378e34e663rbowen ;-) I will provide this, too.</p>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
4aa603e6448b99f9371397d439795c91a93637eand<pre class="prettyprint lang-perl">#!/sw/bin/perl
0f6366523dec7face01fcad3eadcc1378e34e663rbowen##
0f6366523dec7face01fcad3eadcc1378e34e663rbowen## nph-refresh -- NPH/CGI script for auto refreshing pages
0f6366523dec7face01fcad3eadcc1378e34e663rbowen## Copyright (c) 1997 Ralf S. Engelschall, All Rights Reserved.
0f6366523dec7face01fcad3eadcc1378e34e663rbowen##
0f6366523dec7face01fcad3eadcc1378e34e663rbowen$| = 1;
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen# split the QUERY_STRING variable
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh@pairs = split( /&amp;/, $ENV{'QUERY_STRING'} );
0f6366523dec7face01fcad3eadcc1378e34e663rbowenforeach $pair (@pairs) {
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh ( $name, $value ) = split( /=/, $pair );
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh $name =~ tr/A-Z/a-z/;
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh $name = 'QS_' . $name;
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh eval "\$$name = \"$value\"";
0f6366523dec7face01fcad3eadcc1378e34e663rbowen}
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh$QS_s = 1 if ( $QS_s eq '' );
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh$QS_n = 3600 if ( $QS_n eq '' );
ac811ad71a8af43080082a96c032280a05e1a4c3humbedoohif ( $QS_f eq '' ) {
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print "HTTP/1.0 200 OK\n";
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print "Content-type: text/html\n\n";
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print "&lt;b&gt;ERROR&lt;/b&gt;: No file given\n";
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh exit(0);
0f6366523dec7face01fcad3eadcc1378e34e663rbowen}
ac811ad71a8af43080082a96c032280a05e1a4c3humbedoohif ( !-f $QS_f ) {
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print "HTTP/1.0 200 OK\n";
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print "Content-type: text/html\n\n";
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print "&lt;b&gt;ERROR&lt;/b&gt;: File $QS_f not found\n";
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh exit(0);
0f6366523dec7face01fcad3eadcc1378e34e663rbowen}
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowensub print_http_headers_multipart_begin {
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print "HTTP/1.0 200 OK\n";
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh $bound = "ThisRandomString12345";
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print "Content-type: multipart/x-mixed-replace;boundary=$bound\n";
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh &amp;print_http_headers_multipart_next;
0f6366523dec7face01fcad3eadcc1378e34e663rbowen}
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowensub print_http_headers_multipart_next {
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print "\n--$bound\n";
0f6366523dec7face01fcad3eadcc1378e34e663rbowen}
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowensub print_http_headers_multipart_end {
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print "\n--$bound--\n";
0f6366523dec7face01fcad3eadcc1378e34e663rbowen}
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowensub displayhtml {
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh local ($buffer) = @_;
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh $len = length($buffer);
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print "Content-type: text/html\n";
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print "Content-length: $len\n\n";
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh print $buffer;
0f6366523dec7face01fcad3eadcc1378e34e663rbowen}
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowensub readfile {
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh local ($file) = @_;
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh local ( *FP, $size, $buffer, $bytes );
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh ( $x, $x, $x, $x, $x, $x, $x, $size ) = stat($file);
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh $size = sprintf( "%d", $size );
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh open( FP, "&lt;$file" );
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh $bytes = sysread( FP, $buffer, $size );
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh close(FP);
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh return $buffer;
0f6366523dec7face01fcad3eadcc1378e34e663rbowen}
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen$buffer = &amp;readfile($QS_f);
0f6366523dec7face01fcad3eadcc1378e34e663rbowen&amp;print_http_headers_multipart_begin;
0f6366523dec7face01fcad3eadcc1378e34e663rbowen&amp;displayhtml($buffer);
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowensub mystat {
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh local ($file) = $_[0];
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh local ($time);
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh ( $x, $x, $x, $x, $x, $x, $x, $x, $x, $mtime ) = stat($file);
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh return $mtime;
0f6366523dec7face01fcad3eadcc1378e34e663rbowen}
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen$mtimeL = &amp;mystat($QS_f);
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh$mtime = $mtime;
ac811ad71a8af43080082a96c032280a05e1a4c3humbedoohfor ( $n = 0 ; $n &amp; lt ; $QS_n ; $n++ ) {
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh while (1) {
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh $mtime = &amp;mystat($QS_f);
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh if ( $mtime ne $mtimeL ) {
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh $mtimeL = $mtime;
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh sleep(2);
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh $buffer = &amp;readfile($QS_f);
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh &amp;print_http_headers_multipart_next;
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh &amp;displayhtml($buffer);
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh sleep(5);
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh $mtimeL = &amp;mystat($QS_f);
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh last;
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh }
ac811ad71a8af43080082a96c032280a05e1a4c3humbedooh sleep($QS_s);
0f6366523dec7face01fcad3eadcc1378e34e663rbowen }
0f6366523dec7face01fcad3eadcc1378e34e663rbowen}
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowen&amp;print_http_headers_multipart_end;
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
0f6366523dec7face01fcad3eadcc1378e34e663rbowenexit(0);
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
4aa603e6448b99f9371397d439795c91a93637eand##EOF##</pre>
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedooh
0f6366523dec7face01fcad3eadcc1378e34e663rbowen </dd>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen </dl>
0f6366523dec7face01fcad3eadcc1378e34e663rbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen<div class="section">
43289eb78809cbdec92c7d90433a8852233f0b2brbowen<h2><a name="structuredhomedirs" id="structuredhomedirs">Structured Userdirs</a></h2>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dl>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dt>Description:</dt>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <p>Some sites with thousands of users use a
43289eb78809cbdec92c7d90433a8852233f0b2brbowen structured homedir layout, <em>i.e.</em> each homedir is in a
43289eb78809cbdec92c7d90433a8852233f0b2brbowen subdirectory which begins (for instance) with the first
43289eb78809cbdec92c7d90433a8852233f0b2brbowen character of the username. So, <code>/~larry/anypath</code>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen is <code>/home/<strong>l</strong>/larry/public_html/anypath</code>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen while <code>/~waldo/anypath</code> is
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <code>/home/<strong>w</strong>/waldo/public_html/anypath</code>.</p>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dt>Solution:</dt>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <p>We use the following ruleset to expand the tilde URLs
43289eb78809cbdec92c7d90433a8852233f0b2brbowen into the above layout.</p>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
4aa603e6448b99f9371397d439795c91a93637eand<pre class="prettyprint lang-config">RewriteEngine on
4aa603e6448b99f9371397d439795c91a93637eandRewriteRule ^/~(<strong>([a-z])</strong>[a-z0-9]+)(.*) /home/<strong>$2</strong>/$1/public_html$3</pre>
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedooh
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </dl>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen<div class="section">
43289eb78809cbdec92c7d90433a8852233f0b2brbowen<h2><a name="redirectanchors" id="redirectanchors">Redirecting Anchors</a></h2>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dl>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dt>Description:</dt>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <p>By default, redirecting to an HTML anchor doesn't work,
43289eb78809cbdec92c7d90433a8852233f0b2brbowen because mod_rewrite escapes the <code>#</code> character,
43289eb78809cbdec92c7d90433a8852233f0b2brbowen turning it into <code>%23</code>. This, in turn, breaks the
43289eb78809cbdec92c7d90433a8852233f0b2brbowen redirection.</p>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dt>Solution:</dt>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <p>Use the <code>[NE]</code> flag on the
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <code>RewriteRule</code>. NE stands for No Escape.
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </p>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dt>Discussion:</dt>
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf <dd>This technique will of course also work with other
43289eb78809cbdec92c7d90433a8852233f0b2brbowen special characters that mod_rewrite, by default, URL-encodes.</dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </dl>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen<div class="section">
43289eb78809cbdec92c7d90433a8852233f0b2brbowen<h2><a name="time-dependent" id="time-dependent">Time-Dependent Rewriting</a></h2>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dl>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dt>Description:</dt>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <p>We wish to use mod_rewrite to serve different content based on
43289eb78809cbdec92c7d90433a8852233f0b2brbowen the time of day.</p>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dt>Solution:</dt>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <p>There are a lot of variables named <code>TIME_xxx</code>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen for rewrite conditions. In conjunction with the special
43289eb78809cbdec92c7d90433a8852233f0b2brbowen lexicographic comparison patterns <code>&lt;STRING</code>,
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <code>&gt;STRING</code> and <code>=STRING</code> we can
43289eb78809cbdec92c7d90433a8852233f0b2brbowen do time-dependent redirects:</p>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
4aa603e6448b99f9371397d439795c91a93637eand<pre class="prettyprint lang-config">RewriteEngine on
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedoohRewriteCond %{TIME_HOUR}%{TIME_MIN} &gt;0700
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedoohRewriteCond %{TIME_HOUR}%{TIME_MIN} &lt;1900
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedoohRewriteRule ^foo\.html$ foo.day.html [L]
4aa603e6448b99f9371397d439795c91a93637eandRewriteRule ^foo\.html$ foo.night.html</pre>
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedooh
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <p>This provides the content of <code>foo.day.html</code>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen under the URL <code>foo.html</code> from
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <code>07:01-18:59</code> and at the remaining time the
43289eb78809cbdec92c7d90433a8852233f0b2brbowen contents of <code>foo.night.html</code>.</p>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <div class="warning"><code class="module"><a href="/mod/mod_cache.html">mod_cache</a></code>, intermediate proxies
43289eb78809cbdec92c7d90433a8852233f0b2brbowen and browsers may each cache responses and cause the either page to be
43289eb78809cbdec92c7d90433a8852233f0b2brbowen shown outside of the time-window configured.
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <code class="module"><a href="/mod/mod_expires.html">mod_expires</a></code> may be used to control this
43289eb78809cbdec92c7d90433a8852233f0b2brbowen effect. You are, of course, much better off simply serving the
43289eb78809cbdec92c7d90433a8852233f0b2brbowen content dynamically, and customizing it based on the time of day.</div>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </dl>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen<div class="section">
43289eb78809cbdec92c7d90433a8852233f0b2brbowen<h2><a name="setenvvars" id="setenvvars">Set Environment Variables Based On URL Parts</a></h2>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dl>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dt>Description:</dt>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <p>At time, we want to maintain some kind of status when we
43289eb78809cbdec92c7d90433a8852233f0b2brbowen perform a rewrite. For example, you want to make a note that
43289eb78809cbdec92c7d90433a8852233f0b2brbowen you've done that rewrite, so that you can check later to see if a
43289eb78809cbdec92c7d90433a8852233f0b2brbowen request can via that rewrite. One way to do this is by setting an
43289eb78809cbdec92c7d90433a8852233f0b2brbowen environment variable.</p>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dt>Solution:</dt>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <p>Use the [E] flag to set an environment variable.</p>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
4aa603e6448b99f9371397d439795c91a93637eand<pre class="prettyprint lang-config">RewriteEngine on
4aa603e6448b99f9371397d439795c91a93637eandRewriteRule ^/horse/(.*) /pony/$1 [E=<strong>rewritten:1</strong>]</pre>
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedooh
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen <p>Later in your ruleset you might check for this environment
43289eb78809cbdec92c7d90433a8852233f0b2brbowen variable using a RewriteCond:</p>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
4aa603e6448b99f9371397d439795c91a93637eand<pre class="prettyprint lang-config">RewriteCond %{ENV:rewritten} =1</pre>
488228a03efe42d9f0b03334a4753ce79a6dc5cchumbedooh
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
53abc235688d883cfa15cdfec354ba03128f357arbowen <p>Note that environment variables do not survive an external
53abc235688d883cfa15cdfec354ba03128f357arbowen redirect. You might consider using the [CO] flag to set a
53abc235688d883cfa15cdfec354ba03128f357arbowen cookie.</p>
53abc235688d883cfa15cdfec354ba03128f357arbowen
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </dd>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen </dl>
43289eb78809cbdec92c7d90433a8852233f0b2brbowen
1525d3506d73644ae695624f16b15cc73cc1c7acrbowen</div></div>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen<div class="bottomlang">
b6e6d2139d50d64fc4bbd01c4f07d7a4accfec8cnd<p><span>Available Languages: </span><a href="/en/rewrite/advanced.html" title="English">&nbsp;en&nbsp;</a></p>
ed62c84ee0911cfebbd8da319ac00757b555707chumbedooh</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&amp;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>
0d0ba3a410038e179b695446bb149cce6264e0abnd<script type="text/javascript"><!--//--><![CDATA[//><!--
ed62c84ee0911cfebbd8da319ac00757b555707chumbedoohvar comments_shortname = 'httpd';
b6e6d2139d50d64fc4bbd01c4f07d7a4accfec8cndvar comments_identifier = 'http://httpd.apache.org/docs/trunk/rewrite/advanced.html';
0d0ba3a410038e179b695446bb149cce6264e0abnd(function(w, d) {
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
ed62c84ee0911cfebbd8da319ac00757b555707chumbedooh d.write('<div id="comments_thread"><\/div>');
0d0ba3a410038e179b695446bb149cce6264e0abnd var s = d.createElement('script');
0d0ba3a410038e179b695446bb149cce6264e0abnd s.type = 'text/javascript';
0d0ba3a410038e179b695446bb149cce6264e0abnd s.async = true;
ac082aefa89416cbdc9a1836eaf3bed9698201c8humbedooh s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
0d0ba3a410038e179b695446bb149cce6264e0abnd (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
0d0ba3a410038e179b695446bb149cce6264e0abnd }
0d0ba3a410038e179b695446bb149cce6264e0abnd else {
ed62c84ee0911cfebbd8da319ac00757b555707chumbedooh d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
0d0ba3a410038e179b695446bb149cce6264e0abnd }
0d0ba3a410038e179b695446bb149cce6264e0abnd})(window, document);
6dfd50de0ccc3b37ff6eee76bbe7f0a51211a534humbedooh//--><!]]></script></div><div id="footer">
07dc96d063d49299da433f84b5c5681da9bbdf68rbowen<p class="apache">Copyright 2014 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>
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen<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[//><!--
0d0ba3a410038e179b695446bb149cce6264e0abndif (typeof(prettyPrint) !== 'undefined') {
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd prettyPrint();
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd}
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd//--><!]]></script>
b630661b71a5495a95831a05a6a2299fb1c7c771rbowen</body></html>