upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / docs / manual / rewrite / rewrite_guide.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>URL Rewriting Guide - 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.0</p>
16 <img alt="" src="../images/feather.gif" /></div>
17 <div class="up"><a href="./index.html"><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.0</a></div><div id="page-content"><div id="preamble"><h1>URL Rewriting Guide</h1>
20 <div class="toplang">
21 <p><span>Available Languages: </span><a href="../en/rewrite/rewrite_guide.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>.
27     It describes how one can use Apache's <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
28     to solve typical URL-based problems with which webmasters are
29     commonony confronted. We give detailed descriptions on how to
30     solve each problem by configuring URL rewriting rulesets.</p>
31
32     <div class="warning">ATTENTION: Depending on your server configuration
33     it may be necessary to slightly change the examples for your
34     situation, e.g. adding the <code>[PT]</code> flag when
35     additionally using <code class="module"><a href="../mod/mod_alias.html">mod_alias</a></code> and
36     <code class="module"><a href="../mod/mod_userdir.html">mod_userdir</a></code>, etc. Or rewriting a ruleset
37     to fit in <code>.htaccess</code> context instead
38     of per-server context. Always try to understand what a
39     particular ruleset really does before you use it. This
40     avoids many problems.</div>
41
42   </div>
43 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#canonicalurl">Canonical URLs</a></li>
44 <li><img alt="" src="../images/down.gif" /> <a href="#canonicalhost">Canonical Hostnames</a></li>
45 <li><img alt="" src="../images/down.gif" /> <a href="#moveddocroot">Moved <code>DocumentRoot</code></a></li>
46 <li><img alt="" src="../images/down.gif" /> <a href="#trailingslash">Trailing Slash Problem</a></li>
47 <li><img alt="" src="../images/down.gif" /> <a href="#movehomedirs">Move Homedirs to Different Webserver</a></li>
48 <li><img alt="" src="../images/down.gif" /> <a href="#multipledirs">Search pages in more than one directory</a></li>
49 <li><img alt="" src="../images/down.gif" /> <a href="#setenvvars">Set Environment Variables According To URL Parts</a></li>
50 <li><img alt="" src="../images/down.gif" /> <a href="#uservhosts">Virtual User Hosts</a></li>
51 <li><img alt="" src="../images/down.gif" /> <a href="#redirecthome">Redirect Homedirs For Foreigners</a></li>
52 <li><img alt="" src="../images/down.gif" /> <a href="#redirectanchors">Redirecting Anchors</a></li>
53 <li><img alt="" src="../images/down.gif" /> Time-Dependent Rewriting</li>
54 <li><img alt="" src="../images/down.gif" /> Backward Compatibility for YYYY to XXXX migration</li>
55 <li><img alt="" src="../images/down.gif" /> <a href="#content">Content Handling</a></li>
56 <li><img alt="" src="../images/down.gif" /> <a href="#access">Access Restriction</a></li>
57 <li><img alt="" src="../images/down.gif" /> <a href="#other">Other</a></li>
58 </ul><h3>See also</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Module
59 documentation</a></li><li><a href="rewrite_intro.html">mod_rewrite
60 introduction</a></li><li><a href="rewrite_tech.html">Technical details</a></li></ul></div>
61 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
62 <div class="section">
63 <h2><a name="canonicalurl" id="canonicalurl">Canonical URLs</a></h2>
64
65
66
67 <dl>
68  <dt>Description:</dt>
69
70    <dd>
71      <p>On some webservers there are more than one URL for a
72      resource. Usually there are canonical URLs (which should be
73      actually used and distributed) and those which are just
74      shortcuts, internal ones, etc. Independent of which URL the
75      user supplied with the request he should finally see the
76      canonical one only.</p>
77    </dd>
78
79    <dt>Solution:</dt>
80
81      <dd>
82        <p>We do an external HTTP redirect for all non-canonical
83        URLs to fix them in the location view of the Browser and
84        for all subsequent requests. In the example ruleset below
85        we replace <code>/~user</code> by the canonical
86        <code>/u/user</code> and fix a missing trailing slash for
87        <code>/u/user</code>.</p>
88
89 <div class="example"><pre>
90 RewriteRule   ^/<strong>~</strong>([^/]+)/?(.*)    /<strong>u</strong>/$1/$2  [<strong>R</strong>]
91 RewriteRule   ^/([uge])/(<strong>[^/]+</strong>)$  /$1/$2<strong>/</strong>   [<strong>R</strong>]
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="canonicalhost" id="canonicalhost">Canonical Hostnames</a></h2>
99
100       <dl>
101         <dt>Description:</dt>
102
103         <dd>The goal of this rule is to force the use of a particular
104         hostname, in preference to other hostnames which may be used to
105         reach the same site. For example, if you wish to force the use
106         of <strong>www.example.com</strong> instead of
107         <strong>example.com</strong>, you might use a variant of the
108         following recipe.</dd>
109
110         <dt>Solution:</dt>
111
112         <dd>
113 <p>For sites running on a port other than 80:</p>
114 <div class="example"><pre>
115 RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
116 RewriteCond %{HTTP_HOST}   !^$
117 RewriteCond %{SERVER_PORT} !^80$
118 RewriteRule ^/(.*)         http://fully.qualified.domain.name:%{SERVER_PORT}/$1 [L,R]
119 </pre></div>
120
121 <p>And for a site running on port 80</p>
122 <div class="example"><pre>
123 RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
124 RewriteCond %{HTTP_HOST}   !^$
125 RewriteRule ^/(.*)         http://fully.qualified.domain.name/$1 [L,R]
126 </pre></div>
127         </dd>
128       </dl>
129
130     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
131 <div class="section">
132 <h2><a name="moveddocroot" id="moveddocroot">Moved <code>DocumentRoot</code></a></h2>
133
134       
135
136       <dl>
137         <dt>Description:</dt>
138
139         <dd>
140 <p>Usually the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
141 of the webserver directly relates to the URL "<code>/</code>".
142 But often this data is not really of top-level priority. For example,
143 you may wish for visitors, on first entering a site, to go to a
144 particular subdirectory <code>/about/</code>. This may be accomplished
145 using the following ruleset:</p>
146 </dd>
147
148         <dt>Solution:</dt>
149
150         <dd>
151           <p>We redirect the URL <code>/</code> to
152           <code>/about/</code>:
153           </p>
154          
155 <div class="example"><pre>
156 RewriteEngine on
157 RewriteRule   <strong>^/$</strong>  /about/  [<strong>R</strong>]
158 </pre></div>
159
160     <p>Note that this can also be handled using the <code class="directive"><a href="../mod/mod_alias.html#redirectmatch">RedirectMatch</a></code> directive:</p>
161
162 <div class="example"><p><code>
163 RedirectMatch ^/$ http://example.com/e/www/
164 </code></p></div>
165 </dd>
166 </dl>
167
168     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
169 <div class="section">
170 <h2><a name="trailingslash" id="trailingslash">Trailing Slash Problem</a></h2>
171
172       
173
174       <dl>
175         <dt>Description:</dt>
176
177     <dd><p>The vast majority of "trailing slash" problems can be dealt
178     with using the techniques discussed in the <a href="http://httpd.apache.org/docs/misc/FAQ-E.html#set-servername">FAQ
179     entry</a>. However, occasionally, there is a need to use mod_rewrite
180     to handle a case where a missing trailing slash causes a URL to
181     fail. This can happen, for example, after a series of complex
182     rewrite rules.</p>
183     </dd>
184
185         <dt>Solution:</dt>
186
187         <dd>
188           <p>The solution to this subtle problem is to let the server
189           add the trailing slash automatically. To do this
190           correctly we have to use an external redirect, so the
191           browser correctly requests subsequent images etc. If we
192           only did a internal rewrite, this would only work for the
193           directory page, but would go wrong when any images are
194           included into this page with relative URLs, because the
195           browser would request an in-lined object. For instance, a
196           request for <code>image.gif</code> in
197           <code>/~quux/foo/index.html</code> would become
198           <code>/~quux/image.gif</code> without the external
199           redirect!</p>
200
201           <p>So, to do this trick we write:</p>
202
203 <div class="example"><pre>
204 RewriteEngine  on
205 RewriteBase    /~quux/
206 RewriteRule    ^foo<strong>$</strong>  foo<strong>/</strong>  [<strong>R</strong>]
207 </pre></div>
208
209    <p>Alternately, you can put the following in a
210    top-level <code>.htaccess</code> file in the content directory.
211    But note that this creates some processing overhead.</p>
212
213 <div class="example"><pre>
214 RewriteEngine  on
215 RewriteBase    /~quux/
216 RewriteCond    %{REQUEST_FILENAME}  <strong>-d</strong>
217 RewriteRule    ^(.+<strong>[^/]</strong>)$           $1<strong>/</strong>  [R]
218 </pre></div>
219         </dd>
220       </dl>
221
222     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
223 <div class="section">
224 <h2><a name="movehomedirs" id="movehomedirs">Move Homedirs to Different Webserver</a></h2>
225
226       
227
228       <dl>
229         <dt>Description:</dt>
230
231         <dd>
232           <p>Many webmasters have asked for a solution to the
233           following situation: They wanted to redirect just all
234           homedirs on a webserver to another webserver. They usually
235           need such things when establishing a newer webserver which
236           will replace the old one over time.</p>
237         </dd>
238
239         <dt>Solution:</dt>
240
241         <dd>
242           <p>The solution is trivial with <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>.
243           On the old webserver we just redirect all
244           <code>/~user/anypath</code> URLs to
245           <code>http://newserver/~user/anypath</code>.</p>
246
247 <div class="example"><pre>
248 RewriteEngine on
249 RewriteRule   ^/~(.+)  http://<strong>newserver</strong>/~$1  [R,L]
250 </pre></div>
251         </dd>
252       </dl>
253
254     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
255 <div class="section">
256 <h2><a name="multipledirs" id="multipledirs">Search pages in more than one directory</a></h2>
257
258       
259
260       <dl>
261         <dt>Description:</dt>
262
263         <dd>
264           <p>Sometimes it is necessary to let the webserver search
265           for pages in more than one directory. Here MultiViews or
266           other techniques cannot help.</p>
267         </dd>
268
269         <dt>Solution:</dt>
270
271         <dd>
272           <p>We program a explicit ruleset which searches for the
273           files in the directories.</p>
274
275 <div class="example"><pre>
276 RewriteEngine on
277
278 #   first try to find it in custom/...
279 #   ...and if found stop and be happy:
280 RewriteCond         /your/docroot/<strong>dir1</strong>/%{REQUEST_FILENAME}  -f
281 RewriteRule  ^(.+)  /your/docroot/<strong>dir1</strong>/$1  [L]
282
283 #   second try to find it in pub/...
284 #   ...and if found stop and be happy:
285 RewriteCond         /your/docroot/<strong>dir2</strong>/%{REQUEST_FILENAME}  -f
286 RewriteRule  ^(.+)  /your/docroot/<strong>dir2</strong>/$1  [L]
287
288 #   else go on for other Alias or ScriptAlias directives,
289 #   etc.
290 RewriteRule   ^(.+)  -  [PT]
291 </pre></div>
292         </dd>
293       </dl>
294
295     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
296 <div class="section">
297 <h2><a name="setenvvars" id="setenvvars">Set Environment Variables According To URL Parts</a></h2>
298
299       
300
301       <dl>
302         <dt>Description:</dt>
303
304         <dd>
305           <p>Perhaps you want to keep status information between
306           requests and use the URL to encode it. But you don't want
307           to use a CGI wrapper for all pages just to strip out this
308           information.</p>
309         </dd>
310
311         <dt>Solution:</dt>
312
313         <dd>
314           <p>We use a rewrite rule to strip out the status information
315           and remember it via an environment variable which can be
316           later dereferenced from within XSSI or CGI. This way a
317           URL <code>/foo/S=java/bar/</code> gets translated to
318           <code>/foo/bar/</code> and the environment variable named
319           <code>STATUS</code> is set to the value "java".</p>
320
321 <div class="example"><pre>
322 RewriteEngine on
323 RewriteRule   ^(.*)/<strong>S=([^/]+)</strong>/(.*)    $1/$3 [E=<strong>STATUS:$2</strong>]
324 </pre></div>
325         </dd>
326       </dl>
327
328     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
329 <div class="section">
330 <h2><a name="uservhosts" id="uservhosts">Virtual User Hosts</a></h2>
331
332       
333
334       <dl>
335         <dt>Description:</dt>
336
337         <dd>
338           <p>Assume that you want to provide
339           <code>www.<strong>username</strong>.host.domain.com</code>
340           for the homepage of username via just DNS A records to the
341           same machine and without any virtualhosts on this
342           machine.</p>
343         </dd>
344
345         <dt>Solution:</dt>
346
347         <dd>
348           <p>For HTTP/1.0 requests there is no solution, but for
349           HTTP/1.1 requests which contain a Host: HTTP header we
350           can use the following ruleset to rewrite
351           <code>http://www.username.host.com/anypath</code>
352           internally to <code>/home/username/anypath</code>:</p>
353
354 <div class="example"><pre>
355 RewriteEngine on
356 RewriteCond   %{<strong>HTTP_HOST</strong>}                 ^www\.<strong>[^.]+</strong>\.host\.com$
357 RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
358 RewriteRule   ^www\.<strong>([^.]+)</strong>\.host\.com(.*) /home/<strong>$1</strong>$2
359 </pre></div>
360         </dd>
361       </dl>
362
363     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
364 <div class="section">
365 <h2><a name="redirecthome" id="redirecthome">Redirect Homedirs For Foreigners</a></h2>
366
367       
368
369       <dl>
370         <dt>Description:</dt>
371
372         <dd>
373           <p>We want to redirect homedir URLs to another webserver
374           <code>www.somewhere.com</code> when the requesting user
375           does not stay in the local domain
376           <code>ourdomain.com</code>. This is sometimes used in
377           virtual host contexts.</p>
378         </dd>
379
380         <dt>Solution:</dt>
381
382         <dd>
383           <p>Just a rewrite condition:</p>
384
385 <div class="example"><pre>
386 RewriteEngine on
387 RewriteCond   %{REMOTE_HOST}  <strong>!^.+\.ourdomain\.com$</strong>
388 RewriteRule   ^(/~.+)         http://www.somewhere.com/$1 [R,L]
389 </pre></div>
390         </dd>
391       </dl>
392
393     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
394 <div class="section">
395 <h2><a name="redirectanchors" id="redirectanchors">Redirecting Anchors</a></h2>
396
397       
398
399       <dl>
400         <dt>Description:</dt>
401
402         <dd>
403         <p>By default, redirecting to an HTML anchor doesn't work,
404         because mod_rewrite escapes the <code>#</code> character,
405         turning it into <code>%23</code>. This, in turn, breaks the
406         redirection.</p>
407         </dd>
408
409         <dt>Solution:</dt>
410
411         <dd>
412           <p>Use the <code>[NE]</code> flag on the
413           <code>RewriteRule</code>. NE stands for No Escape.
414           </p>
415         </dd>
416       </dl>
417
418     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
419 <div class="section">
420 <h2>Time-Dependent Rewriting</h2>
421
422       
423
424       <dl>
425         <dt>Description:</dt>
426
427         <dd>
428           <p>When tricks like time-dependent content should happen a
429           lot of webmasters still use CGI scripts which do for
430           instance redirects to specialized pages. How can it be done
431           via <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>?</p>
432         </dd>
433
434         <dt>Solution:</dt>
435
436         <dd>
437           <p>There are a lot of variables named <code>TIME_xxx</code>
438           for rewrite conditions. In conjunction with the special
439           lexicographic comparison patterns <code>&lt;STRING</code>,
440           <code>&gt;STRING</code> and <code>=STRING</code> we can
441           do time-dependent redirects:</p>
442
443 <div class="example"><pre>
444 RewriteEngine on
445 RewriteCond   %{TIME_HOUR}%{TIME_MIN} &gt;0700
446 RewriteCond   %{TIME_HOUR}%{TIME_MIN} &lt;1900
447 RewriteRule   ^foo\.html$             foo.day.html
448 RewriteRule   ^foo\.html$             foo.night.html
449 </pre></div>
450
451           <p>This provides the content of <code>foo.day.html</code>
452           under the URL <code>foo.html</code> from
453           <code>07:00-19:00</code> and at the remaining time the
454           contents of <code>foo.night.html</code>. Just a nice
455           feature for a homepage...</p>
456         </dd>
457       </dl>
458
459     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
460 <div class="section">
461 <h2>Backward Compatibility for YYYY to XXXX migration</h2>
462
463       
464
465       <dl>
466         <dt>Description:</dt>
467
468         <dd>
469           <p>How can we make URLs backward compatible (still
470           existing virtually) after migrating <code>document.YYYY</code>
471           to <code>document.XXXX</code>, e.g. after translating a
472           bunch of <code>.html</code> files to <code>.phtml</code>?</p>
473         </dd>
474
475         <dt>Solution:</dt>
476
477         <dd>
478           <p>We just rewrite the name to its basename and test for
479           existence of the new extension. If it exists, we take
480           that name, else we rewrite the URL to its original state.</p>
481
482
483 <div class="example"><pre>
484 #   backward compatibility ruleset for
485 #   rewriting document.html to document.phtml
486 #   when and only when document.phtml exists
487 #   but no longer document.html
488 RewriteEngine on
489 RewriteBase   /~quux/
490 #   parse out basename, but remember the fact
491 RewriteRule   ^(.*)\.html$              $1      [C,E=WasHTML:yes]
492 #   rewrite to document.phtml if exists
493 RewriteCond   %{REQUEST_FILENAME}.phtml -f
494 RewriteRule   ^(.*)$ $1.phtml                   [S=1]
495 #   else reverse the previous basename cutout
496 RewriteCond   %{ENV:WasHTML}            ^yes$
497 RewriteRule   ^(.*)$ $1.html
498 </pre></div>
499         </dd>
500       </dl>
501
502     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
503 <div class="section">
504 <h2><a name="content" id="content">Content Handling</a></h2>
505
506     
507
508     <h3>From Old to New (intern)</h3>
509
510       
511
512       <dl>
513         <dt>Description:</dt>
514
515         <dd>
516           <p>Assume we have recently renamed the page
517           <code>foo.html</code> to <code>bar.html</code> and now want
518           to provide the old URL for backward compatibility. Actually
519           we want that users of the old URL even not recognize that
520           the pages was renamed.</p>
521         </dd>
522
523         <dt>Solution:</dt>
524
525         <dd>
526           <p>We rewrite the old URL to the new one internally via the
527           following rule:</p>
528
529 <div class="example"><pre>
530 RewriteEngine  on
531 RewriteBase    /~quux/
532 RewriteRule    ^<strong>foo</strong>\.html$  <strong>bar</strong>.html
533 </pre></div>
534         </dd>
535       </dl>
536
537     
538
539     <h3>From Old to New (extern)</h3>
540
541       
542
543       <dl>
544         <dt>Description:</dt>
545
546         <dd>
547           <p>Assume again that we have recently renamed the page
548           <code>foo.html</code> to <code>bar.html</code> and now want
549           to provide the old URL for backward compatibility. But this
550           time we want that the users of the old URL get hinted to
551           the new one, i.e. their browsers Location field should
552           change, too.</p>
553         </dd>
554
555         <dt>Solution:</dt>
556
557         <dd>
558           <p>We force a HTTP redirect to the new URL which leads to a
559           change of the browsers and thus the users view:</p>
560
561 <div class="example"><pre>
562 RewriteEngine  on
563 RewriteBase    /~quux/
564 RewriteRule    ^<strong>foo</strong>\.html$  <strong>bar</strong>.html  [<strong>R</strong>]
565 </pre></div>
566         </dd>
567       </dl>
568
569     
570
571     <h3>From Static to Dynamic</h3>
572
573       
574
575       <dl>
576         <dt>Description:</dt>
577
578         <dd>
579           <p>How can we transform a static page
580           <code>foo.html</code> into a dynamic variant
581           <code>foo.cgi</code> in a seamless way, i.e. without notice
582           by the browser/user.</p>
583         </dd>
584
585         <dt>Solution:</dt>
586
587         <dd>
588           <p>We just rewrite the URL to the CGI-script and force the
589           correct MIME-type so it gets really run as a CGI-script.
590           This way a request to <code>/~quux/foo.html</code>
591           internally leads to the invocation of
592           <code>/~quux/foo.cgi</code>.</p>
593
594 <div class="example"><pre>
595 RewriteEngine  on
596 RewriteBase    /~quux/
597 RewriteRule    ^foo\.<strong>html</strong>$  foo.<strong>cgi</strong>  [T=<strong>application/x-httpd-cgi</strong>]
598 </pre></div>
599         </dd>
600       </dl>
601
602     
603 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
604 <div class="section">
605 <h2><a name="access" id="access">Access Restriction</a></h2>
606
607     
608
609     <h3>Blocking of Robots</h3>
610
611       
612
613       <dl>
614         <dt>Description:</dt>
615
616         <dd>
617           <p>How can we block a really annoying robot from
618           retrieving pages of a specific webarea? A
619           <code>/robots.txt</code> file containing entries of the
620           "Robot Exclusion Protocol" is typically not enough to get
621           rid of such a robot.</p>
622         </dd>
623
624         <dt>Solution:</dt>
625
626         <dd>
627           <p>We use a ruleset which forbids the URLs of the webarea
628           <code>/~quux/foo/arc/</code> (perhaps a very deep
629           directory indexed area where the robot traversal would
630           create big server load). We have to make sure that we
631           forbid access only to the particular robot, i.e. just
632           forbidding the host where the robot runs is not enough.
633           This would block users from this host, too. We accomplish
634           this by also matching the User-Agent HTTP header
635           information.</p>
636
637 <div class="example"><pre>
638 RewriteCond %{HTTP_USER_AGENT}   ^<strong>NameOfBadRobot</strong>.*
639 RewriteCond %{REMOTE_ADDR}       ^<strong>123\.45\.67\.[8-9]</strong>$
640 RewriteRule ^<strong>/~quux/foo/arc/</strong>.+   -   [<strong>F</strong>]
641 </pre></div>
642         </dd>
643       </dl>
644
645     
646
647     <h3>Blocked Inline-Images</h3>
648
649       
650
651       <dl>
652         <dt>Description:</dt>
653
654         <dd>
655           <p>Assume we have under <code>http://www.quux-corp.de/~quux/</code>
656           some pages with inlined GIF graphics. These graphics are
657           nice, so others directly incorporate them via hyperlinks to
658           their pages. We don't like this practice because it adds
659           useless traffic to our server.</p>
660         </dd>
661
662         <dt>Solution:</dt>
663
664         <dd>
665           <p>While we cannot 100% protect the images from inclusion,
666           we can at least restrict the cases where the browser
667           sends a HTTP Referer header.</p>
668
669 <div class="example"><pre>
670 RewriteCond %{HTTP_REFERER} <strong>!^$</strong>
671 RewriteCond %{HTTP_REFERER} !^http://www.quux-corp.de/~quux/.*$ [NC]
672 RewriteRule <strong>.*\.gif$</strong>        -                                    [F]
673 </pre></div>
674
675 <div class="example"><pre>
676 RewriteCond %{HTTP_REFERER}         !^$
677 RewriteCond %{HTTP_REFERER}         !.*/foo-with-gif\.html$
678 RewriteRule <strong>^inlined-in-foo\.gif$</strong>   -                        [F]
679 </pre></div>
680         </dd>
681       </dl>
682
683     
684
685     <h3>Proxy Deny</h3>
686
687       
688
689       <dl>
690         <dt>Description:</dt>
691
692         <dd>
693           <p>How can we forbid a certain host or even a user of a
694           special host from using the Apache proxy?</p>
695         </dd>
696
697         <dt>Solution:</dt>
698
699         <dd>
700           <p>We first have to make sure <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
701           is below(!) <code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code> in the Configuration
702           file when compiling the Apache webserver. This way it gets
703           called <em>before</em> <code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code>. Then we
704           configure the following for a host-dependent deny...</p>
705
706 <div class="example"><pre>
707 RewriteCond %{REMOTE_HOST} <strong>^badhost\.mydomain\.com$</strong>
708 RewriteRule !^http://[^/.]\.mydomain.com.*  - [F]
709 </pre></div>
710
711           <p>...and this one for a user@host-dependent deny:</p>
712
713 <div class="example"><pre>
714 RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST}  <strong>^badguy@badhost\.mydomain\.com$</strong>
715 RewriteRule !^http://[^/.]\.mydomain.com.*  - [F]
716 </pre></div>
717         </dd>
718       </dl>
719
720     
721
722   </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
723 <div class="section">
724 <h2><a name="other" id="other">Other</a></h2>
725
726     
727
728     <h3>External Rewriting Engine</h3>
729
730       
731
732       <dl>
733         <dt>Description:</dt>
734
735         <dd>
736           <p>A FAQ: How can we solve the FOO/BAR/QUUX/etc.
737           problem? There seems no solution by the use of
738           <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>...</p>
739         </dd>
740
741         <dt>Solution:</dt>
742
743         <dd>
744           <p>Use an external <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>, i.e. a program which acts
745           like a <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>. It is run once on startup of Apache
746           receives the requested URLs on <code>STDIN</code> and has
747           to put the resulting (usually rewritten) URL on
748           <code>STDOUT</code> (same order!).</p>
749
750 <div class="example"><pre>
751 RewriteEngine on
752 RewriteMap    quux-map       <strong>prg:</strong>/path/to/map.quux.pl
753 RewriteRule   ^/~quux/(.*)$  /~quux/<strong>${quux-map:$1}</strong>
754 </pre></div>
755
756 <div class="example"><pre>
757 #!/path/to/perl
758
759 #   disable buffered I/O which would lead
760 #   to deadloops for the Apache server
761 $| = 1;
762
763 #   read URLs one per line from stdin and
764 #   generate substitution URL on stdout
765 while (&lt;&gt;) {
766     s|^foo/|bar/|;
767     print $_;
768 }
769 </pre></div>
770
771           <p>This is a demonstration-only example and just rewrites
772           all URLs <code>/~quux/foo/...</code> to
773           <code>/~quux/bar/...</code>. Actually you can program
774           whatever you like. But notice that while such maps can be
775           <strong>used</strong> also by an average user, only the
776           system administrator can <strong>define</strong> it.</p>
777         </dd>
778       </dl>
779
780     
781
782   </div></div>
783 <div class="bottomlang">
784 <p><span>Available Languages: </span><a href="../en/rewrite/rewrite_guide.html" title="English">&nbsp;en&nbsp;</a></p>
785 </div><div id="footer">
786 <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>
787 <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>
788 </body></html>