1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5 This file is generated from xml source: DO NOT EDIT
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
8 <title>When not to use mod_rewrite - Apache HTTP Server</title>
9 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11 <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
12 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
13 <body id="manual-page"><div id="page-header">
14 <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>
15 <p class="apache">Apache HTTP Server Version 2.3</p>
16 <img alt="" src="../images/feather.gif" /></div>
17 <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
19 <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>When not to use mod_rewrite</h1>
21 <p><span>Available Languages: </span><a href="../en/rewrite/avoid.html" title="English"> en </a></p>
25 <p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
26 <a href="../mod/mod_rewrite.html">reference documentation</a>. It provides
27 a few advanced techniques and tricks using mod_rewrite.</p>
29 <div class="warning">Note that many of these examples won't work unchanged in your
30 particular server configuration, so it's important that you understand
31 them, rather than merely cutting and pasting the examples into your
35 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#sharding">URL-based sharding accross multiple backends</a></li>
36 <li><img alt="" src="../images/down.gif" /> <a href="#on-the-fly-content">On-the-fly Content-Regeneration</a></li>
37 <li><img alt="" src="../images/down.gif" /> <a href="#load-balancing">Load Balancing</a></li>
38 </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>
39 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
41 <h2><a name="sharding" id="sharding">URL-based sharding accross multiple backends</a></h2>
49 <p>A common technique for distributing the burden of
50 server load or storage space is called "sharding".
51 When using this method, a front-end server will use the
52 url to consistently "shard" users or objects to separate
59 <p>A mapping is maintained, from users to target servers, in
60 external map files. They look like:</p>
62 <div class="example"><pre>
63 user1 physical_host_of_user1
64 user2 physical_host_of_user2
68 <p>We put this into a <code>map.users-to-hosts</code> file. The
71 <div class="example"><pre>
77 <div class="example"><pre>
78 http://physical_host_of_user1/u/user/anypath
81 <p>thus every URL path need not be valid on every backend physical
82 host. The following ruleset does this for us with the help of the map
83 files assuming that server0 is a default server which will be used if
84 a user has no entry in the map):</p>
86 <div class="example"><pre>
89 RewriteMap users-to-hosts txt:/path/to/map.users-to-hosts
91 RewriteRule ^/u/<strong>([^/]+)</strong>/?(.*) http://<strong>${users-to-hosts:$1|server0}</strong>/u/$1/$2
96 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
98 <h2><a name="on-the-fly-content" id="on-the-fly-content">On-the-fly Content-Regeneration</a></h2>
103 <dt>Description:</dt>
106 <p>We wish to dynamically generate content, but store it
107 statically once it is generated. This rule will check for the
108 existence of the static file, and if it's not there, generate
109 it. The static files can be removed periodically, if desired (say,
110 via cron) and will be regenerated on demand.</p>
116 This is done via the following ruleset:
118 <div class="example"><pre>
119 # This example is valid in per-directory context only
120 RewriteCond %{REQUEST_FILENAME} <strong>!-s</strong>
121 RewriteRule ^page\.<strong>html</strong>$ page.<strong>cgi</strong> [T=application/x-httpd-cgi,L]
124 <p>Here a request for <code>page.html</code> leads to an
125 internal run of a corresponding <code>page.cgi</code> if
126 <code>page.html</code> is missing or has filesize
127 null. The trick here is that <code>page.cgi</code> is a
128 CGI script which (additionally to its <code>STDOUT</code>)
129 writes its output to the file <code>page.html</code>.
130 Once it has completed, the server sends out
131 <code>page.html</code>. When the webmaster wants to force
132 a refresh of the contents, he just removes
133 <code>page.html</code> (typically from <code>cron</code>).</p>
137 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
138 <div class="section">
139 <h2><a name="load-balancing" id="load-balancing">Load Balancing</a></h2>
144 <dt>Description:</dt>
147 <p>We wish to randomly distribute load across several servers
148 using mod_rewrite.</p>
154 <p>We'll use <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> and a list of servers
155 to accomplish this.</p>
157 <div class="example"><pre>
159 RewriteMap lb rnd:/path/to/serverlist.txt
161 RewriteRule ^/(.*) http://${lb:servers}/$1 [P,L]
164 <p><code>serverlist.txt</code> will contain a list of the servers:</p>
166 <div class="example"><pre>
169 servers one.example.com|two.example.com|three.example.com
172 <p>If you want one particular server to get more of the load than the
173 others, add it more times to the list.</p>
179 <p>Apache comes with a load-balancing module -
180 <code class="module"><a href="../mod/mod_proxy_balancer.html">mod_proxy_balancer</a></code> - which is far more flexible and
181 featureful than anything you can cobble together using mod_rewrite.</p>
186 <div class="bottomlang">
187 <p><span>Available Languages: </span><a href="../en/rewrite/avoid.html" title="English"> en </a></p>
188 </div><div id="footer">
189 <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>
190 <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>