advanced.html.en revision 8b79a240383bcecf28b75d4803683a7d101d2713
<?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><!--
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
This file is generated from xml source: DO NOT EDIT
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-->
<title>When not to use mod_rewrite - Apache HTTP Server</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="/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>
<img alt="" src="/images/feather.gif" /></div>
<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="/images/left.gif" /></a></div>
<div id="path">
<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.3</a> &gt; <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>When not to use mod_rewrite</h1>
<div class="toplang">
<p><span>Available Languages: </span><a href="/en/rewrite/avoid.html" title="English">&nbsp;en&nbsp;</a></p>
</div>
<p>This document supplements the <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
<a href="/mod_rewrite.html">reference documentation</a>. It provides
a few advanced techniques and tricks using mod_rewrite.</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
them, rather than merely cutting and pasting the examples into your
configuration.</div>
</div>
<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#sharding">URL-based sharding accross multiple backends</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#on-the-fly-content">On-the-fly Content-Regeneration</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="avoid.html">When not to use mod_rewrite</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="sharding" id="sharding">URL-based sharding accross multiple backends</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>A common technique for distributing the burden of
server load or storage space is called "sharding".
When using this method, a front-end server will use the
url to consistently "shard" users or objects to separate
backend servers.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>A mapping is maintained, from users to target servers, in
external map files. They look like:</p>
<div class="example"><pre>
user1 physical_host_of_user1
user2 physical_host_of_user2
: :
</pre></div>
<p>We put this into a <code>map.users-to-hosts</code> file. The
aim is to map;</p>
<div class="example"><pre>
/u/user1/anypath
</pre></div>
<p>to</p>
<div class="example"><pre>
http://physical_host_of_user1/u/user/anypath
</pre></div>
<p>thus every URL path need not be valid on every backend physical
host. The following ruleset does this for us with the help of the map
files assuming that server0 is a default server which will be used if
a user has no entry in the map):</p>
<div class="example"><pre>
RewriteEngine on
RewriteMap users-to-hosts txt:/path/to/map.users-to-hosts
RewriteRule ^/u/<strong>([^/]+)</strong>/?(.*) http://<strong>${users-to-hosts:$1|server0}</strong>/u/$1/$2
</pre></div>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="on-the-fly-content" id="on-the-fly-content">On-the-fly Content-Regeneration</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>We wish to dynamically generate content, but store it
statically once it is generated. This rule will check for the
existence of the static file, and if it's not there, generate
it. The static files can be removed periodically, if desired (say,
via cron) and will be regenerated on demand.</p>
</dd>
<dt>Solution:</dt>
<dd>
This is done via the following ruleset:
<div class="example"><pre>
# This example is valid in per-directory context only
RewriteCond %{REQUEST_FILENAME} <strong>!-s</strong>
RewriteRule ^page\.<strong>html</strong>$ page.<strong>cgi</strong> [T=application/x-httpd-cgi,L]
</pre></div>
<p>Here a request for <code>page.html</code> leads to an
internal run of a corresponding <code>page.cgi</code> if
<code>page.html</code> is missing or has filesize
null. The trick here is that <code>page.cgi</code> is a
CGI script which (additionally to its <code>STDOUT</code>)
writes its output to the file <code>page.html</code>.
Once it has completed, the server sends out
<code>page.html</code>. When the webmaster wants to force
a refresh of the contents, he just removes
<code>page.html</code> (typically from <code>cron</code>).</p>
</dd>
</dl>
</div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="/en/rewrite/avoid.html" title="English">&nbsp;en&nbsp;</a></p>
</div><div id="footer">
<p class="apache">Copyright 2009 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>
</body></html>