]> granicus.if.org Git - apache/blob - docs/manual/rewrite/advanced.html.en
Corrects the page title.
[apache] / docs / manual / rewrite / advanced.html.en
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
7       -->
8 <title>Advanced Techniques with 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="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
18 <div id="path">
19 <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>Advanced Techniques with mod_rewrite</h1>
20 <div class="toplang">
21 <p><span>Available Languages: </span><a href="../en/rewrite/avoid.html" title="English">&nbsp;en&nbsp;</a></p>
22 </div>
23
24
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>
28
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
32 configuration.</div>
33
34 </div>
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>
40 <div class="section">
41 <h2><a name="sharding" id="sharding">URL-based sharding accross multiple backends</a></h2>
42
43   
44
45   <dl>
46     <dt>Description:</dt>
47
48     <dd>
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
53       backend servers.</p>
54     </dd>
55
56     <dt>Solution:</dt>
57
58     <dd>
59       <p>A mapping is maintained, from users to target servers, in
60       external map files. They look like:</p>
61
62 <div class="example"><pre>
63 user1  physical_host_of_user1
64 user2  physical_host_of_user2
65 :      :
66 </pre></div>
67
68   <p>We put this into a <code>map.users-to-hosts</code> file. The
69     aim is to map;</p>
70
71 <div class="example"><pre>
72 /u/user1/anypath
73 </pre></div>
74
75   <p>to</p>
76
77 <div class="example"><pre>
78 http://physical_host_of_user1/u/user/anypath
79 </pre></div>
80
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>
85
86 <div class="example"><pre>
87 RewriteEngine on
88
89 RewriteMap      users-to-hosts   txt:/path/to/map.users-to-hosts
90
91 RewriteRule   ^/u/<strong>([^/]+)</strong>/?(.*)   http://<strong>${users-to-hosts:$1|server0}</strong>/u/$1/$2
92 </pre></div>
93     </dd>
94   </dl>
95
96 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
97 <div class="section">
98 <h2><a name="on-the-fly-content" id="on-the-fly-content">On-the-fly Content-Regeneration</a></h2>
99
100   
101
102   <dl>
103     <dt>Description:</dt>
104
105     <dd>
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>
111     </dd>
112
113     <dt>Solution:</dt>
114
115     <dd>
116       This is done via the following ruleset:
117
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]
122 </pre></div>
123
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>
134     </dd>
135   </dl>
136
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>
140
141   
142
143   <dl>
144     <dt>Description:</dt>
145
146     <dd>
147       <p>We wish to randomly distribute load across several servers
148       using mod_rewrite.</p>
149     </dd>
150
151     <dt>Solution:</dt>
152
153     <dd>
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>
156
157 <div class="example"><pre>
158 RewriteEngine on
159 RewriteMap lb rnd:/path/to/serverlist.txt
160
161 RewriteRule ^/(.*) http://${lb:servers}/$1 [P,L]
162 </pre></div>
163
164 <p><code>serverlist.txt</code> will contain a list of the servers:</p>
165
166 <div class="example"><pre>
167 ## serverlist.txt
168
169 servers one.example.com|two.example.com|three.example.com
170 </pre></div>
171
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>
174
175    </dd>
176
177    <dt>Discussion</dt>
178    <dd>
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>
182    </dd>
183   </dl>
184
185 </div></div>
186 <div class="bottomlang">
187 <p><span>Available Languages: </span><a href="../en/rewrite/avoid.html" title="English">&nbsp;en&nbsp;</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>
191 </body></html>