home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / apache_2.2.8-win32-x86-no_ssl.msi / Data1.cab / _AE4DB9B0AEED4A1E086815446E2B5E1E < prev    next >
Extensible Markup Language  |  2007-12-07  |  73KB  |  2,102 lines

  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.2</p>
  16. <img alt="" src="../images/feather.gif" /></div>
  17. <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
  18. <div id="path">
  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.2</a> > <a href="./">Miscellaneous Documentation</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/misc/rewriteguide.html" title="English"> en </a> |
  22. <a href="../ko/misc/rewriteguide.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
  23. </div>
  24.  
  25.     <div class="note">
  26.       <p>Originally written by<br />
  27.       <cite>Ralf S. Engelschall <rse@apache.org></cite><br />
  28.       December 1997</p>
  29.     </div>
  30.  
  31.     <p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
  32.     <a href="../mod/mod_rewrite.html">reference documentation</a>.
  33.     It describes how one can use Apache's <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
  34.     to solve typical URL-based problems with which webmasters are
  35.     commonly confronted. We give detailed descriptions on how to
  36.     solve each problem by configuring URL rewriting rulesets.</p>
  37.  
  38.   </div>
  39. <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#ToC1">Introduction to <code>mod_rewrite</code></a></li>
  40. <li><img alt="" src="../images/down.gif" /> <a href="#ToC2">Practical Solutions</a></li>
  41. <li><img alt="" src="../images/down.gif" /> <a href="#url">URL Layout</a></li>
  42. <li><img alt="" src="../images/down.gif" /> <a href="#content">Content Handling</a></li>
  43. <li><img alt="" src="../images/down.gif" /> <a href="#access">Access Restriction</a></li>
  44. <li><img alt="" src="../images/down.gif" /> <a href="#other">Other</a></li>
  45. </ul></div>
  46. <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  47. <div class="section">
  48. <h2><a name="ToC1" id="ToC1">Introduction to <code>mod_rewrite</code></a></h2>
  49.  
  50.     
  51.  
  52.     <p>The Apache module <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> is a killer
  53.     one, i.e. it is a really sophisticated module which provides
  54.     a powerful way to do URL manipulations. With it you can do nearly 
  55.     all types of URL manipulations you ever dreamed about.
  56.     The price you have to pay is to accept complexity, because
  57.     <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>'s major drawback is that it is
  58.     not easy to understand and use for the beginner. And even
  59.     Apache experts sometimes discover new aspects where
  60.     <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> can help.</p>
  61.  
  62.     <p>In other words: With <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> you either
  63.     shoot yourself in the foot the first time and never use it again
  64.     or love it for the rest of your life because of its power.
  65.     This paper tries to give you a few initial success events to
  66.     avoid the first case by presenting already invented solutions
  67.     to you.</p>
  68.  
  69.   </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  70. <div class="section">
  71. <h2><a name="ToC2" id="ToC2">Practical Solutions</a></h2>
  72.  
  73.     
  74.  
  75.     <p>Here come a lot of practical solutions I've either invented
  76.     myself or collected from other people's solutions in the past.
  77.     Feel free to learn the black magic of URL rewriting from
  78.     these examples.</p>
  79.  
  80.     <div class="warning">ATTENTION: Depending on your server-configuration
  81.     it can be necessary to slightly change the examples for your
  82.     situation, e.g. adding the <code>[PT]</code> flag when
  83.     additionally using <code class="module"><a href="../mod/mod_alias.html">mod_alias</a></code> and
  84.     <code class="module"><a href="../mod/mod_userdir.html">mod_userdir</a></code>, etc. Or rewriting a ruleset
  85.     to fit in <code>.htaccess</code> context instead
  86.     of per-server context. Always try to understand what a
  87.     particular ruleset really does before you use it. It
  88.     avoid problems.</div>
  89.  
  90.   </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  91. <div class="section">
  92. <h2><a name="url" id="url">URL Layout</a></h2>
  93.  
  94.     
  95.  
  96.     <h3>Canonical URLs</h3>
  97.  
  98.       
  99.  
  100.       <dl>
  101.         <dt>Description:</dt>
  102.  
  103.         <dd>
  104.           <p>On some webservers there are more than one URL for a
  105.           resource. Usually there are canonical URLs (which should be
  106.           actually used and distributed) and those which are just
  107.           shortcuts, internal ones, etc. Independent of which URL the
  108.           user supplied with the request he should finally see the
  109.           canonical one only.</p>
  110.         </dd>
  111.  
  112.         <dt>Solution:</dt>
  113.  
  114.         <dd>
  115.           <p>We do an external HTTP redirect for all non-canonical
  116.           URLs to fix them in the location view of the Browser and
  117.           for all subsequent requests. In the example ruleset below
  118.           we replace <code>/~user</code> by the canonical
  119.           <code>/u/user</code> and fix a missing trailing slash for
  120.           <code>/u/user</code>.</p>
  121.  
  122. <div class="example"><pre>
  123. RewriteRule   ^/<strong>~</strong>([^/]+)/?(.*)    /<strong>u</strong>/$1/$2  [<strong>R</strong>]
  124. RewriteRule   ^/([uge])/(<strong>[^/]+</strong>)$  /$1/$2<strong>/</strong>   [<strong>R</strong>]
  125. </pre></div>
  126.         </dd>
  127.       </dl>
  128.  
  129.     
  130.  
  131.     <h3>Canonical Hostnames</h3>
  132.  
  133.       
  134.  
  135.       <dl>
  136.         <dt>Description:</dt>
  137.  
  138.         <dd>The goal of this rule is to force the use of a particular
  139.         hostname, in preference to other hostnames which may be used to
  140.         reach the same site. For example, if you wish to force the use
  141.         of <strong>www.example.com</strong> instead of
  142.         <strong>example.com</strong>, you might use a variant of the
  143.         following recipe.</dd>
  144.  
  145.  
  146.         <dt>Solution:</dt>
  147.  
  148.         <dd>
  149. <div class="example"><pre>
  150. # To force the use of 
  151. RewriteEngine On
  152. RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]
  153. RewriteCond %{HTTP_HOST}   !^$
  154. RewriteRule ^/(.*)         http://www.example.com/$1 [L,R]
  155. </pre></div>
  156.         </dd>
  157.       </dl>
  158.  
  159.     
  160.  
  161.     <h3>Moved <code>DocumentRoot</code></h3>
  162.  
  163.       
  164.  
  165.       <dl>
  166.         <dt>Description:</dt>
  167.  
  168.         <dd>
  169.           <p>Usually the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
  170.           of the webserver directly relates to the URL "<code>/</code>".
  171.           But often this data is not really of top-level priority, it is
  172.           perhaps just one entity of a lot of data pools. For instance at
  173.           our Intranet sites there are <code>/e/www/</code>
  174.           (the homepage for WWW), <code>/e/sww/</code> (the homepage for
  175.           the Intranet) etc. Now because the data of the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code> stays at <code>/e/www/</code> we had
  176.           to make sure that all inlined images and other stuff inside this
  177.           data pool work for subsequent requests.</p>
  178.         </dd>
  179.  
  180.         <dt>Solution:</dt>
  181.  
  182.         <dd>
  183.           <p>We redirect the URL <code>/</code> to
  184.           <code>/e/www/</code>:
  185.           </p>
  186.          
  187. <div class="example"><pre>
  188. RewriteEngine on
  189. RewriteRule   <strong>^/$</strong>  /e/www/  [<strong>R</strong>]
  190. </pre></div>
  191.  
  192.     <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>
  193.  
  194.     <div class="example"><p><code>
  195.     RedirectMatch ^/$ http://example.com/e/www/
  196.     </code></p></div>
  197.         </dd>
  198.       </dl>
  199.  
  200.     
  201.  
  202.     <h3>Trailing Slash Problem</h3>
  203.  
  204.       
  205.  
  206.       <dl>
  207.         <dt>Description:</dt>
  208.  
  209.         <dd>
  210.           <p>Every webmaster can sing a song about the problem of
  211.           the trailing slash on URLs referencing directories. If they
  212.           are missing, the server dumps an error, because if you say
  213.           <code>/~quux/foo</code> instead of <code>/~quux/foo/</code>
  214.           then the server searches for a <em>file</em> named
  215.           <code>foo</code>. And because this file is a directory it
  216.           complains. Actually it tries to fix it itself in most of
  217.           the cases, but sometimes this mechanism need to be emulated
  218.           by you. For instance after you have done a lot of
  219.           complicated URL rewritings to CGI scripts etc.</p>
  220.         </dd>
  221.  
  222.         <dt>Solution:</dt>
  223.  
  224.         <dd>
  225.           <p>The solution to this subtle problem is to let the server
  226.           add the trailing slash automatically. To do this
  227.           correctly we have to use an external redirect, so the
  228.           browser correctly requests subsequent images etc. If we
  229.           only did a internal rewrite, this would only work for the
  230.           directory page, but would go wrong when any images are
  231.           included into this page with relative URLs, because the
  232.           browser would request an in-lined object. For instance, a
  233.           request for <code>image.gif</code> in
  234.           <code>/~quux/foo/index.html</code> would become
  235.           <code>/~quux/image.gif</code> without the external
  236.           redirect!</p>
  237.  
  238.           <p>So, to do this trick we write:</p>
  239.  
  240. <div class="example"><pre>
  241. RewriteEngine  on
  242. RewriteBase    /~quux/
  243. RewriteRule    ^foo<strong>$</strong>  foo<strong>/</strong>  [<strong>R</strong>]
  244. </pre></div>
  245.  
  246.           <p>The crazy and lazy can even do the following in the
  247.           top-level <code>.htaccess</code> file of their homedir.
  248.           But notice that this creates some processing
  249.           overhead.</p>
  250.  
  251. <div class="example"><pre>
  252. RewriteEngine  on
  253. RewriteBase    /~quux/
  254. RewriteCond    %{REQUEST_FILENAME}  <strong>-d</strong>
  255. RewriteRule    ^(.+<strong>[^/]</strong>)$           $1<strong>/</strong>  [R]
  256. </pre></div>
  257.         </dd>
  258.       </dl>
  259.  
  260.     
  261.  
  262.     <h3>Webcluster through Homogeneous URL Layout</h3>
  263.  
  264.       
  265.  
  266.       <dl>
  267.         <dt>Description:</dt>
  268.  
  269.         <dd>
  270.           <p>We want to create a homogeneous and consistent URL
  271.           layout over all WWW servers on a Intranet webcluster, i.e.
  272.           all URLs (per definition server local and thus server
  273.           dependent!) become actually server <em>independent</em>!
  274.           What we want is to give the WWW namespace a consistent
  275.           server-independent layout: no URL should have to include
  276.           any physically correct target server. The cluster itself
  277.           should drive us automatically to the physical target
  278.           host.</p>
  279.         </dd>
  280.  
  281.         <dt>Solution:</dt>
  282.  
  283.         <dd>
  284.           <p>First, the knowledge of the target servers come from
  285.           (distributed) external maps which contain information
  286.           where our users, groups and entities stay. The have the
  287.           form</p>
  288.  
  289. <div class="example"><pre>
  290. user1  server_of_user1
  291. user2  server_of_user2
  292. :      :
  293. </pre></div>
  294.  
  295.           <p>We put them into files <code>map.xxx-to-host</code>.
  296.           Second we need to instruct all servers to redirect URLs
  297.           of the forms</p>
  298.  
  299. <div class="example"><pre>
  300. /u/user/anypath
  301. /g/group/anypath
  302. /e/entity/anypath
  303. </pre></div>
  304.  
  305.           <p>to</p>
  306.  
  307. <div class="example"><pre>
  308. http://physical-host/u/user/anypath
  309. http://physical-host/g/group/anypath
  310. http://physical-host/e/entity/anypath
  311. </pre></div>
  312.  
  313.           <p>when the URL is not locally valid to a server. The
  314.           following ruleset does this for us by the help of the map
  315.           files (assuming that server0 is a default server which
  316.           will be used if a user has no entry in the map):</p>
  317.  
  318. <div class="example"><pre>
  319. RewriteEngine on
  320.  
  321. RewriteMap      user-to-host   txt:/path/to/map.user-to-host
  322. RewriteMap     group-to-host   txt:/path/to/map.group-to-host
  323. RewriteMap    entity-to-host   txt:/path/to/map.entity-to-host
  324.  
  325. RewriteRule   ^/u/<strong>([^/]+)</strong>/?(.*)   http://<strong>${user-to-host:$1|server0}</strong>/u/$1/$2
  326. RewriteRule   ^/g/<strong>([^/]+)</strong>/?(.*)  http://<strong>${group-to-host:$1|server0}</strong>/g/$1/$2
  327. RewriteRule   ^/e/<strong>([^/]+)</strong>/?(.*) http://<strong>${entity-to-host:$1|server0}</strong>/e/$1/$2
  328.  
  329. RewriteRule   ^/([uge])/([^/]+)/?$          /$1/$2/.www/
  330. RewriteRule   ^/([uge])/([^/]+)/([^.]+.+)   /$1/$2/.www/$3\
  331. </pre></div>
  332.         </dd>
  333.       </dl>
  334.  
  335.     
  336.  
  337.     <h3>Move Homedirs to Different Webserver</h3>
  338.  
  339.       
  340.  
  341.       <dl>
  342.         <dt>Description:</dt>
  343.  
  344.         <dd>
  345.           <p>Many webmasters have asked for a solution to the
  346.           following situation: They wanted to redirect just all
  347.           homedirs on a webserver to another webserver. They usually
  348.           need such things when establishing a newer webserver which
  349.           will replace the old one over time.</p>
  350.         </dd>
  351.  
  352.         <dt>Solution:</dt>
  353.  
  354.         <dd>
  355.           <p>The solution is trivial with <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>.
  356.           On the old webserver we just redirect all
  357.           <code>/~user/anypath</code> URLs to
  358.           <code>http://newserver/~user/anypath</code>.</p>
  359.  
  360. <div class="example"><pre>
  361. RewriteEngine on
  362. RewriteRule   ^/~(.+)  http://<strong>newserver</strong>/~$1  [R,L]
  363. </pre></div>
  364.         </dd>
  365.       </dl>
  366.  
  367.     
  368.  
  369.     <h3>Structured Homedirs</h3>
  370.  
  371.       
  372.  
  373.       <dl>
  374.         <dt>Description:</dt>
  375.  
  376.         <dd>
  377.           <p>Some sites with thousands of users usually use a
  378.           structured homedir layout, i.e. each homedir is in a
  379.           subdirectory which begins for instance with the first
  380.           character of the username. So, <code>/~foo/anypath</code>
  381.           is <code>/home/<strong>f</strong>/foo/.www/anypath</code>
  382.           while <code>/~bar/anypath</code> is
  383.           <code>/home/<strong>b</strong>/bar/.www/anypath</code>.</p>
  384.         </dd>
  385.  
  386.         <dt>Solution:</dt>
  387.  
  388.         <dd>
  389.           <p>We use the following ruleset to expand the tilde URLs
  390.           into exactly the above layout.</p>
  391.  
  392. <div class="example"><pre>
  393. RewriteEngine on
  394. RewriteRule   ^/~(<strong>([a-z])</strong>[a-z0-9]+)(.*)  /home/<strong>$2</strong>/$1/.www$3
  395. </pre></div>
  396.         </dd>
  397.       </dl>
  398.  
  399.     
  400.  
  401.     <h3>Filesystem Reorganization</h3>
  402.  
  403.       
  404.  
  405.       <dl>
  406.         <dt>Description:</dt>
  407.  
  408.         <dd>
  409.           <p>This really is a hardcore example: a killer application
  410.           which heavily uses per-directory
  411.           <code>RewriteRules</code> to get a smooth look and feel
  412.           on the Web while its data structure is never touched or
  413.           adjusted. Background: <strong><em>net.sw</em></strong> is
  414.           my archive of freely available Unix software packages,
  415.           which I started to collect in 1992. It is both my hobby
  416.           and job to to this, because while I'm studying computer
  417.           science I have also worked for many years as a system and
  418.           network administrator in my spare time. Every week I need
  419.           some sort of software so I created a deep hierarchy of
  420.           directories where I stored the packages:</p>
  421.  
  422. <div class="example"><pre>
  423. drwxrwxr-x   2 netsw  users    512 Aug  3 18:39 Audio/
  424. drwxrwxr-x   2 netsw  users    512 Jul  9 14:37 Benchmark/
  425. drwxrwxr-x  12 netsw  users    512 Jul  9 00:34 Crypto/
  426. drwxrwxr-x   5 netsw  users    512 Jul  9 00:41 Database/
  427. drwxrwxr-x   4 netsw  users    512 Jul 30 19:25 Dicts/
  428. drwxrwxr-x  10 netsw  users    512 Jul  9 01:54 Graphic/
  429. drwxrwxr-x   5 netsw  users    512 Jul  9 01:58 Hackers/
  430. drwxrwxr-x   8 netsw  users    512 Jul  9 03:19 InfoSys/
  431. drwxrwxr-x   3 netsw  users    512 Jul  9 03:21 Math/
  432. drwxrwxr-x   3 netsw  users    512 Jul  9 03:24 Misc/
  433. drwxrwxr-x   9 netsw  users    512 Aug  1 16:33 Network/
  434. drwxrwxr-x   2 netsw  users    512 Jul  9 05:53 Office/
  435. drwxrwxr-x   7 netsw  users    512 Jul  9 09:24 SoftEng/
  436. drwxrwxr-x   7 netsw  users    512 Jul  9 12:17 System/
  437. drwxrwxr-x  12 netsw  users    512 Aug  3 20:15 Typesetting/
  438. drwxrwxr-x  10 netsw  users    512 Jul  9 14:08 X11/
  439. </pre></div>
  440.  
  441.           <p>In July 1996 I decided to make this archive public to
  442.           the world via a nice Web interface. "Nice" means that I
  443.           wanted to offer an interface where you can browse
  444.           directly through the archive hierarchy. And "nice" means
  445.           that I didn't wanted to change anything inside this
  446.           hierarchy - not even by putting some CGI scripts at the
  447.           top of it. Why? Because the above structure should be
  448.           later accessible via FTP as well, and I didn't want any
  449.           Web or CGI stuff to be there.</p>
  450.         </dd>
  451.  
  452.         <dt>Solution:</dt>
  453.  
  454.         <dd>
  455.           <p>The solution has two parts: The first is a set of CGI
  456.           scripts which create all the pages at all directory
  457.           levels on-the-fly. I put them under
  458.           <code>/e/netsw/.www/</code> as follows:</p>
  459.  
  460. <div class="example"><pre>
  461. -rw-r--r--   1 netsw  users    1318 Aug  1 18:10 .wwwacl
  462. drwxr-xr-x  18 netsw  users     512 Aug  5 15:51 DATA/
  463. -rw-rw-rw-   1 netsw  users  372982 Aug  5 16:35 LOGFILE
  464. -rw-r--r--   1 netsw  users     659 Aug  4 09:27 TODO
  465. -rw-r--r--   1 netsw  users    5697 Aug  1 18:01 netsw-about.html
  466. -rwxr-xr-x   1 netsw  users     579 Aug  2 10:33 netsw-access.pl
  467. -rwxr-xr-x   1 netsw  users    1532 Aug  1 17:35 netsw-changes.cgi
  468. -rwxr-xr-x   1 netsw  users    2866 Aug  5 14:49 netsw-home.cgi
  469. drwxr-xr-x   2 netsw  users     512 Jul  8 23:47 netsw-img/
  470. -rwxr-xr-x   1 netsw  users   24050 Aug  5 15:49 netsw-lsdir.cgi
  471. -rwxr-xr-x   1 netsw  users    1589 Aug  3 18:43 netsw-search.cgi
  472. -rwxr-xr-x   1 netsw  users    1885 Aug  1 17:41 netsw-tree.cgi
  473. -rw-r--r--   1 netsw  users     234 Jul 30 16:35 netsw-unlimit.lst
  474. </pre></div>
  475.  
  476.           <p>The <code>DATA/</code> subdirectory holds the above
  477.           directory structure, i.e. the real
  478.           <strong><em>net.sw</em></strong> stuff and gets
  479.           automatically updated via <code>rdist</code> from time to
  480.           time. The second part of the problem remains: how to link
  481.           these two structures together into one smooth-looking URL
  482.           tree? We want to hide the <code>DATA/</code> directory
  483.           from the user while running the appropriate CGI scripts
  484.           for the various URLs. Here is the solution: first I put
  485.           the following into the per-directory configuration file
  486.           in the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
  487.           of the server to rewrite the announced URL
  488.           <code>/net.sw/</code> to the internal path
  489.           <code>/e/netsw</code>:</p>
  490.  
  491. <div class="example"><pre>
  492. RewriteRule  ^net.sw$       net.sw/        [R]
  493. RewriteRule  ^net.sw/(.*)$  e/netsw/$1
  494. </pre></div>
  495.  
  496.           <p>The first rule is for requests which miss the trailing
  497.           slash! The second rule does the real thing. And then
  498.           comes the killer configuration which stays in the
  499.           per-directory config file
  500.           <code>/e/netsw/.www/.wwwacl</code>:</p>
  501.  
  502. <div class="example"><pre>
  503. Options       ExecCGI FollowSymLinks Includes MultiViews
  504.  
  505. RewriteEngine on
  506.  
  507. #  we are reached via /net.sw/ prefix
  508. RewriteBase   /net.sw/
  509.  
  510. #  first we rewrite the root dir to
  511. #  the handling cgi script
  512. RewriteRule   ^$                       netsw-home.cgi     [L]
  513. RewriteRule   ^index\.html$            netsw-home.cgi     [L]
  514.  
  515. #  strip out the subdirs when
  516. #  the browser requests us from perdir pages
  517. RewriteRule   ^.+/(netsw-[^/]+/.+)$    $1                 [L]
  518.  
  519. #  and now break the rewriting for local files
  520. RewriteRule   ^netsw-home\.cgi.*       -                  [L]
  521. RewriteRule   ^netsw-changes\.cgi.*    -                  [L]
  522. RewriteRule   ^netsw-search\.cgi.*     -                  [L]
  523. RewriteRule   ^netsw-tree\.cgi$        -                  [L]
  524. RewriteRule   ^netsw-about\.html$      -                  [L]
  525. RewriteRule   ^netsw-img/.*$           -                  [L]
  526.  
  527. #  anything else is a subdir which gets handled
  528. #  by another cgi script
  529. RewriteRule   !^netsw-lsdir\.cgi.*     -                  [C]
  530. RewriteRule   (.*)                     netsw-lsdir.cgi/$1
  531. </pre></div>
  532.  
  533.           <p>Some hints for interpretation:</p>
  534.  
  535.           <ol>
  536.             <li>Notice the <code>L</code> (last) flag and no
  537.             substitution field ('<code>-</code>') in the forth part</li>
  538.  
  539.             <li>Notice the <code>!</code> (not) character and
  540.             the <code>C</code> (chain) flag at the first rule
  541.             in the last part</li>
  542.  
  543.             <li>Notice the catch-all pattern in the last rule</li>
  544.           </ol>
  545.         </dd>
  546.       </dl>
  547.  
  548.     
  549.  
  550.     <h3>NCSA imagemap to Apache <code>mod_imagemap</code></h3>
  551.  
  552.       
  553.  
  554.       <dl>
  555.         <dt>Description:</dt>
  556.  
  557.         <dd>
  558.           <p>When switching from the NCSA webserver to the more
  559.           modern Apache webserver a lot of people want a smooth
  560.           transition. So they want pages which use their old NCSA
  561.           <code>imagemap</code> program to work under Apache with the
  562.           modern <code class="module"><a href="../mod/mod_imagemap.html">mod_imagemap</a></code>. The problem is that there
  563.           are a lot of hyperlinks around which reference the
  564.           <code>imagemap</code> program via
  565.           <code>/cgi-bin/imagemap/path/to/page.map</code>. Under
  566.           Apache this has to read just
  567.           <code>/path/to/page.map</code>.</p>
  568.         </dd>
  569.  
  570.         <dt>Solution:</dt>
  571.  
  572.         <dd>
  573.           <p>We use a global rule to remove the prefix on-the-fly for
  574.           all requests:</p>
  575.  
  576. <div class="example"><pre>
  577. RewriteEngine  on
  578. RewriteRule    ^/cgi-bin/imagemap(.*)  $1  [PT]
  579. </pre></div>
  580.         </dd>
  581.       </dl>
  582.  
  583.     
  584.  
  585.     <h3>Search pages in more than one directory</h3>
  586.  
  587.       
  588.  
  589.       <dl>
  590.         <dt>Description:</dt>
  591.  
  592.         <dd>
  593.           <p>Sometimes it is necessary to let the webserver search
  594.           for pages in more than one directory. Here MultiViews or
  595.           other techniques cannot help.</p>
  596.         </dd>
  597.  
  598.         <dt>Solution:</dt>
  599.  
  600.         <dd>
  601.           <p>We program a explicit ruleset which searches for the
  602.           files in the directories.</p>
  603.  
  604. <div class="example"><pre>
  605. RewriteEngine on
  606.  
  607. #   first try to find it in custom/...
  608. #   ...and if found stop and be happy:
  609. RewriteCond         /your/docroot/<strong>dir1</strong>/%{REQUEST_FILENAME}  -f
  610. RewriteRule  ^(.+)  /your/docroot/<strong>dir1</strong>/$1  [L]
  611.  
  612. #   second try to find it in pub/...
  613. #   ...and if found stop and be happy:
  614. RewriteCond         /your/docroot/<strong>dir2</strong>/%{REQUEST_FILENAME}  -f
  615. RewriteRule  ^(.+)  /your/docroot/<strong>dir2</strong>/$1  [L]
  616.  
  617. #   else go on for other Alias or ScriptAlias directives,
  618. #   etc.
  619. RewriteRule   ^(.+)  -  [PT]
  620. </pre></div>
  621.         </dd>
  622.       </dl>
  623.  
  624.     
  625.  
  626.     <h3>Set Environment Variables According To URL Parts</h3>
  627.  
  628.       
  629.  
  630.       <dl>
  631.         <dt>Description:</dt>
  632.  
  633.         <dd>
  634.           <p>Perhaps you want to keep status information between
  635.           requests and use the URL to encode it. But you don't want
  636.           to use a CGI wrapper for all pages just to strip out this
  637.           information.</p>
  638.         </dd>
  639.  
  640.         <dt>Solution:</dt>
  641.  
  642.         <dd>
  643.           <p>We use a rewrite rule to strip out the status information
  644.           and remember it via an environment variable which can be
  645.           later dereferenced from within XSSI or CGI. This way a
  646.           URL <code>/foo/S=java/bar/</code> gets translated to
  647.           <code>/foo/bar/</code> and the environment variable named
  648.           <code>STATUS</code> is set to the value "java".</p>
  649.  
  650. <div class="example"><pre>
  651. RewriteEngine on
  652. RewriteRule   ^(.*)/<strong>S=([^/]+)</strong>/(.*)    $1/$3 [E=<strong>STATUS:$2</strong>]
  653. </pre></div>
  654.         </dd>
  655.       </dl>
  656.  
  657.     
  658.  
  659.     <h3>Virtual User Hosts</h3>
  660.  
  661.       
  662.  
  663.       <dl>
  664.         <dt>Description:</dt>
  665.  
  666.         <dd>
  667.           <p>Assume that you want to provide
  668.           <code>www.<strong>username</strong>.host.domain.com</code>
  669.           for the homepage of username via just DNS A records to the
  670.           same machine and without any virtualhosts on this
  671.           machine.</p>
  672.         </dd>
  673.  
  674.         <dt>Solution:</dt>
  675.  
  676.         <dd>
  677.           <p>For HTTP/1.0 requests there is no solution, but for
  678.           HTTP/1.1 requests which contain a Host: HTTP header we
  679.           can use the following ruleset to rewrite
  680.           <code>http://www.username.host.com/anypath</code>
  681.           internally to <code>/home/username/anypath</code>:</p>
  682.  
  683. <div class="example"><pre>
  684. RewriteEngine on
  685. RewriteCond   %{<strong>HTTP_HOST</strong>}                 ^www\.<strong>[^.]+</strong>\.host\.com$
  686. RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
  687. RewriteRule   ^www\.<strong>([^.]+)</strong>\.host\.com(.*) /home/<strong>$1</strong>$2
  688. </pre></div>
  689.         </dd>
  690.       </dl>
  691.  
  692.     
  693.  
  694.     <h3>Redirect Homedirs For Foreigners</h3>
  695.  
  696.       
  697.  
  698.       <dl>
  699.         <dt>Description:</dt>
  700.  
  701.         <dd>
  702.           <p>We want to redirect homedir URLs to another webserver
  703.           <code>www.somewhere.com</code> when the requesting user
  704.           does not stay in the local domain
  705.           <code>ourdomain.com</code>. This is sometimes used in
  706.           virtual host contexts.</p>
  707.         </dd>
  708.  
  709.         <dt>Solution:</dt>
  710.  
  711.         <dd>
  712.           <p>Just a rewrite condition:</p>
  713.  
  714. <div class="example"><pre>
  715. RewriteEngine on
  716. RewriteCond   %{REMOTE_HOST}  <strong>!^.+\.ourdomain\.com$</strong>
  717. RewriteRule   ^(/~.+)         http://www.somewhere.com/$1 [R,L]
  718. </pre></div>
  719.         </dd>
  720.       </dl>
  721.  
  722.     
  723.  
  724.     <h3>Redirect Failing URLs To Other Webserver</h3>
  725.  
  726.       
  727.  
  728.       <dl>
  729.         <dt>Description:</dt>
  730.  
  731.         <dd>
  732.           <p>A typical FAQ about URL rewriting is how to redirect
  733.           failing requests on webserver A to webserver B. Usually
  734.           this is done via <code class="directive"><a href="../mod/core.html#errordocument">ErrorDocument</a></code> CGI-scripts in Perl, but
  735.           there is also a <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> solution.
  736.           But notice that this performs more poorly than using an
  737.           <code class="directive"><a href="../mod/core.html#errordocument">ErrorDocument</a></code>
  738.           CGI-script!</p>
  739.         </dd>
  740.  
  741.         <dt>Solution:</dt>
  742.  
  743.         <dd>
  744.           <p>The first solution has the best performance but less
  745.           flexibility, and is less error safe:</p>
  746.  
  747. <div class="example"><pre>
  748. RewriteEngine on
  749. RewriteCond   /your/docroot/%{REQUEST_FILENAME} <strong>!-f</strong>
  750. RewriteRule   ^(.+)                             http://<strong>webserverB</strong>.dom/$1
  751. </pre></div>
  752.  
  753.           <p>The problem here is that this will only work for pages
  754.           inside the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>. While you can add more
  755.           Conditions (for instance to also handle homedirs, etc.)
  756.           there is better variant:</p>
  757.  
  758. <div class="example"><pre>
  759. RewriteEngine on
  760. RewriteCond   %{REQUEST_URI} <strong>!-U</strong>
  761. RewriteRule   ^(.+)          http://<strong>webserverB</strong>.dom/$1
  762. </pre></div>
  763.  
  764.           <p>This uses the URL look-ahead feature of <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>.
  765.           The result is that this will work for all types of URLs
  766.           and is a safe way. But it does a performance impact on
  767.           the webserver, because for every request there is one
  768.           more internal subrequest. So, if your webserver runs on a
  769.           powerful CPU, use this one. If it is a slow machine, use
  770.           the first approach or better a <code class="directive"><a href="../mod/core.html#errordocument">ErrorDocument</a></code> CGI-script.</p>
  771.         </dd>
  772.       </dl>
  773.  
  774.     
  775.  
  776.     <h3>Extended Redirection</h3>
  777.  
  778.       
  779.  
  780.       <dl>
  781.         <dt>Description:</dt>
  782.  
  783.         <dd>
  784.           <p>Sometimes we need more control (concerning the
  785.           character escaping mechanism) of URLs on redirects.
  786.           Usually the Apache kernels URL escape function also
  787.           escapes anchors, i.e. URLs like "<code>url#anchor</code>".
  788.           You cannot use this directly on redirects with
  789.           <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> because the
  790.           <code>uri_escape()</code> function of Apache
  791.           would also escape the hash character.
  792.           How can we redirect to such a URL?</p>
  793.         </dd>
  794.  
  795.         <dt>Solution:</dt>
  796.  
  797.         <dd>
  798.           <p>We have to use a kludge by the use of a NPH-CGI script
  799.           which does the redirect itself. Because here no escaping
  800.           is done (NPH=non-parseable headers). First we introduce a
  801.           new URL scheme <code>xredirect:</code> by the following
  802.           per-server config-line (should be one of the last rewrite
  803.           rules):</p>
  804.  
  805. <div class="example"><pre>
  806. RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 \
  807.             [T=application/x-httpd-cgi,L]
  808. </pre></div>
  809.  
  810.           <p>This forces all URLs prefixed with
  811.           <code>xredirect:</code> to be piped through the
  812.           <code>nph-xredirect.cgi</code> program. And this program
  813.           just looks like:</p>
  814.  
  815. <div class="example"><pre>
  816. #!/path/to/perl
  817. ##
  818. ##  nph-xredirect.cgi -- NPH/CGI script for extended redirects
  819. ##  Copyright (c) 1997 Ralf S. Engelschall, All Rights Reserved.
  820. ##
  821.  
  822. $| = 1;
  823. $url = $ENV{'PATH_INFO'};
  824.  
  825. print "HTTP/1.0 302 Moved Temporarily\n";
  826. print "Server: $ENV{'SERVER_SOFTWARE'}\n";
  827. print "Location: $url\n";
  828. print "Content-type: text/html\n";
  829. print "\n";
  830. print "<html>\n";
  831. print "<head>\n";
  832. print "<title>302 Moved Temporarily (EXTENDED)</title>\n";
  833. print "</head>\n";
  834. print "<body>\n";
  835. print "<h1>Moved Temporarily (EXTENDED)</h1>\n";
  836. print "The document has moved <a HREF=\"$url\">here</a>.<p>\n";
  837. print "</body>\n";
  838. print "</html>\n";
  839.  
  840. ##EOF##
  841. </pre></div>
  842.  
  843.           <p>This provides you with the functionality to do
  844.           redirects to all URL schemes, i.e. including the one
  845.           which are not directly accepted by <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>.
  846.           For instance you can now also redirect to
  847.           <code>news:newsgroup</code> via</p>
  848.  
  849. <div class="example"><pre>
  850. RewriteRule ^anyurl  xredirect:news:newsgroup
  851. </pre></div>
  852.  
  853.           <div class="note">Notice: You have not to put <code>[R]</code> or
  854.           <code>[R,L]</code> to the above rule because the
  855.           <code>xredirect:</code> need to be expanded later
  856.           by our special "pipe through" rule above.</div>
  857.         </dd>
  858.       </dl>
  859.  
  860.     
  861.  
  862.     <h3>Archive Access Multiplexer</h3>
  863.  
  864.       
  865.  
  866.       <dl>
  867.         <dt>Description:</dt>
  868.  
  869.         <dd>
  870.           <p>Do you know the great CPAN (Comprehensive Perl Archive
  871.           Network) under <a href="http://www.perl.com/CPAN">http://www.perl.com/CPAN</a>?
  872.           This does a redirect to one of several FTP servers around
  873.           the world which carry a CPAN mirror and is approximately
  874.           near the location of the requesting client. Actually this
  875.           can be called an FTP access multiplexing service. While
  876.           CPAN runs via CGI scripts, how can a similar approach
  877.           implemented via <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>?</p>
  878.         </dd>
  879.  
  880.         <dt>Solution:</dt>
  881.  
  882.         <dd>
  883.           <p>First we notice that from version 3.0.0
  884.           <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> can
  885.           also use the "<code>ftp:</code>" scheme on redirects.
  886.           And second, the location approximation can be done by a
  887.           <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
  888.           over the top-level domain of the client.
  889.           With a tricky chained ruleset we can use this top-level
  890.           domain as a key to our multiplexing map.</p>
  891.  
  892. <div class="example"><pre>
  893. RewriteEngine on
  894. RewriteMap    multiplex                txt:/path/to/map.cxan
  895. RewriteRule   ^/CxAN/(.*)              %{REMOTE_HOST}::$1                 [C]
  896. RewriteRule   ^.+\.<strong>([a-zA-Z]+)</strong>::(.*)$  ${multiplex:<strong>$1</strong>|ftp.default.dom}$2  [R,L]
  897. </pre></div>
  898.  
  899. <div class="example"><pre>
  900. ##
  901. ##  map.cxan -- Multiplexing Map for CxAN
  902. ##
  903.  
  904. de        ftp://ftp.cxan.de/CxAN/
  905. uk        ftp://ftp.cxan.uk/CxAN/
  906. com       ftp://ftp.cxan.com/CxAN/
  907.  :
  908. ##EOF##
  909. </pre></div>
  910.         </dd>
  911.       </dl>
  912.  
  913.     
  914.  
  915.     <h3>Time-Dependent Rewriting</h3>
  916.  
  917.       
  918.  
  919.       <dl>
  920.         <dt>Description:</dt>
  921.  
  922.         <dd>
  923.           <p>When tricks like time-dependent content should happen a
  924.           lot of webmasters still use CGI scripts which do for
  925.           instance redirects to specialized pages. How can it be done
  926.           via <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>?</p>
  927.         </dd>
  928.  
  929.         <dt>Solution:</dt>
  930.  
  931.         <dd>
  932.           <p>There are a lot of variables named <code>TIME_xxx</code>
  933.           for rewrite conditions. In conjunction with the special
  934.           lexicographic comparison patterns <code><STRING</code>,
  935.           <code>>STRING</code> and <code>=STRING</code> we can
  936.           do time-dependent redirects:</p>
  937.  
  938. <div class="example"><pre>
  939. RewriteEngine on
  940. RewriteCond   %{TIME_HOUR}%{TIME_MIN} >0700
  941. RewriteCond   %{TIME_HOUR}%{TIME_MIN} <1900
  942. RewriteRule   ^foo\.html$             foo.day.html
  943. RewriteRule   ^foo\.html$             foo.night.html
  944. </pre></div>
  945.  
  946.           <p>This provides the content of <code>foo.day.html</code>
  947.           under the URL <code>foo.html</code> from
  948.           <code>07:00-19:00</code> and at the remaining time the
  949.           contents of <code>foo.night.html</code>. Just a nice
  950.           feature for a homepage...</p>
  951.         </dd>
  952.       </dl>
  953.  
  954.     
  955.  
  956.     <h3>Backward Compatibility for YYYY to XXXX migration</h3>
  957.  
  958.       
  959.  
  960.       <dl>
  961.         <dt>Description:</dt>
  962.  
  963.         <dd>
  964.           <p>How can we make URLs backward compatible (still
  965.           existing virtually) after migrating <code>document.YYYY</code>
  966.           to <code>document.XXXX</code>, e.g. after translating a
  967.           bunch of <code>.html</code> files to <code>.phtml</code>?</p>
  968.         </dd>
  969.  
  970.         <dt>Solution:</dt>
  971.  
  972.         <dd>
  973.           <p>We just rewrite the name to its basename and test for
  974.           existence of the new extension. If it exists, we take
  975.           that name, else we rewrite the URL to its original state.</p>
  976.  
  977.  
  978. <div class="example"><pre>
  979. #   backward compatibility ruleset for
  980. #   rewriting document.html to document.phtml
  981. #   when and only when document.phtml exists
  982. #   but no longer document.html
  983. RewriteEngine on
  984. RewriteBase   /~quux/
  985. #   parse out basename, but remember the fact
  986. RewriteRule   ^(.*)\.html$              $1      [C,E=WasHTML:yes]
  987. #   rewrite to document.phtml if exists
  988. RewriteCond   %{REQUEST_FILENAME}.phtml -f
  989. RewriteRule   ^(.*)$ $1.phtml                   [S=1]
  990. #   else reverse the previous basename cutout
  991. RewriteCond   %{ENV:WasHTML}            ^yes$
  992. RewriteRule   ^(.*)$ $1.html
  993. </pre></div>
  994.         </dd>
  995.       </dl>
  996.  
  997.     
  998.  
  999.   </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  1000. <div class="section">
  1001. <h2><a name="content" id="content">Content Handling</a></h2>
  1002.  
  1003.     
  1004.  
  1005.     <h3>From Old to New (intern)</h3>
  1006.  
  1007.       
  1008.  
  1009.       <dl>
  1010.         <dt>Description:</dt>
  1011.  
  1012.         <dd>
  1013.           <p>Assume we have recently renamed the page
  1014.           <code>foo.html</code> to <code>bar.html</code> and now want
  1015.           to provide the old URL for backward compatibility. Actually
  1016.           we want that users of the old URL even not recognize that
  1017.           the pages was renamed.</p>
  1018.         </dd>
  1019.  
  1020.         <dt>Solution:</dt>
  1021.  
  1022.         <dd>
  1023.           <p>We rewrite the old URL to the new one internally via the
  1024.           following rule:</p>
  1025.  
  1026. <div class="example"><pre>
  1027. RewriteEngine  on
  1028. RewriteBase    /~quux/
  1029. RewriteRule    ^<strong>foo</strong>\.html$  <strong>bar</strong>.html
  1030. </pre></div>
  1031.         </dd>
  1032.       </dl>
  1033.  
  1034.     
  1035.  
  1036.     <h3>From Old to New (extern)</h3>
  1037.  
  1038.       
  1039.  
  1040.       <dl>
  1041.         <dt>Description:</dt>
  1042.  
  1043.         <dd>
  1044.           <p>Assume again that we have recently renamed the page
  1045.           <code>foo.html</code> to <code>bar.html</code> and now want
  1046.           to provide the old URL for backward compatibility. But this
  1047.           time we want that the users of the old URL get hinted to
  1048.           the new one, i.e. their browsers Location field should
  1049.           change, too.</p>
  1050.         </dd>
  1051.  
  1052.         <dt>Solution:</dt>
  1053.  
  1054.         <dd>
  1055.           <p>We force a HTTP redirect to the new URL which leads to a
  1056.           change of the browsers and thus the users view:</p>
  1057.  
  1058. <div class="example"><pre>
  1059. RewriteEngine  on
  1060. RewriteBase    /~quux/
  1061. RewriteRule    ^<strong>foo</strong>\.html$  <strong>bar</strong>.html  [<strong>R</strong>]
  1062. </pre></div>
  1063.         </dd>
  1064.       </dl>
  1065.  
  1066.     
  1067.  
  1068.     <h3>Browser Dependent Content</h3>
  1069.  
  1070.       
  1071.  
  1072.       <dl>
  1073.         <dt>Description:</dt>
  1074.  
  1075.         <dd>
  1076.           <p>At least for important top-level pages it is sometimes
  1077.           necessary to provide the optimum of browser dependent
  1078.           content, i.e. one has to provide a maximum version for the
  1079.           latest Netscape variants, a minimum version for the Lynx
  1080.           browsers and a average feature version for all others.</p>
  1081.         </dd>
  1082.  
  1083.         <dt>Solution:</dt>
  1084.  
  1085.         <dd>
  1086.           <p>We cannot use content negotiation because the browsers do
  1087.           not provide their type in that form. Instead we have to
  1088.           act on the HTTP header "User-Agent". The following condig
  1089.           does the following: If the HTTP header "User-Agent"
  1090.           begins with "Mozilla/3", the page <code>foo.html</code>
  1091.           is rewritten to <code>foo.NS.html</code> and and the
  1092.           rewriting stops. If the browser is "Lynx" or "Mozilla" of
  1093.           version 1 or 2 the URL becomes <code>foo.20.html</code>.
  1094.           All other browsers receive page <code>foo.32.html</code>.
  1095.           This is done by the following ruleset:</p>
  1096.  
  1097. <div class="example"><pre>
  1098. RewriteCond %{HTTP_USER_AGENT}  ^<strong>Mozilla/3</strong>.*
  1099. RewriteRule ^foo\.html$         foo.<strong>NS</strong>.html          [<strong>L</strong>]
  1100.  
  1101. RewriteCond %{HTTP_USER_AGENT}  ^<strong>Lynx/</strong>.*         [OR]
  1102. RewriteCond %{HTTP_USER_AGENT}  ^<strong>Mozilla/[12]</strong>.*
  1103. RewriteRule ^foo\.html$         foo.<strong>20</strong>.html          [<strong>L</strong>]
  1104.  
  1105. RewriteRule ^foo\.html$         foo.<strong>32</strong>.html          [<strong>L</strong>]
  1106. </pre></div>
  1107.         </dd>
  1108.       </dl>
  1109.  
  1110.     
  1111.  
  1112.     <h3>Dynamic Mirror</h3>
  1113.  
  1114.       
  1115.  
  1116.       <dl>
  1117.         <dt>Description:</dt>
  1118.  
  1119.         <dd>
  1120.           <p>Assume there are nice webpages on remote hosts we want
  1121.           to bring into our namespace. For FTP servers we would use
  1122.           the <code>mirror</code> program which actually maintains an
  1123.           explicit up-to-date copy of the remote data on the local
  1124.           machine. For a webserver we could use the program
  1125.           <code>webcopy</code> which acts similar via HTTP. But both
  1126.           techniques have one major drawback: The local copy is
  1127.           always just as up-to-date as often we run the program. It
  1128.           would be much better if the mirror is not a static one we
  1129.           have to establish explicitly. Instead we want a dynamic
  1130.           mirror with data which gets updated automatically when
  1131.           there is need (updated data on the remote host).</p>
  1132.         </dd>
  1133.  
  1134.         <dt>Solution:</dt>
  1135.  
  1136.         <dd>
  1137.           <p>To provide this feature we map the remote webpage or even
  1138.           the complete remote webarea to our namespace by the use
  1139.           of the <dfn>Proxy Throughput</dfn> feature
  1140.           (flag <code>[P]</code>):</p>
  1141.  
  1142. <div class="example"><pre>
  1143. RewriteEngine  on
  1144. RewriteBase    /~quux/
  1145. RewriteRule    ^<strong>hotsheet/</strong>(.*)$  <strong>http://www.tstimpreso.com/hotsheet/</strong>$1  [<strong>P</strong>]
  1146. </pre></div>
  1147.  
  1148. <div class="example"><pre>
  1149. RewriteEngine  on
  1150. RewriteBase    /~quux/
  1151. RewriteRule    ^<strong>usa-news\.html</strong>$   <strong>http://www.quux-corp.com/news/index.html</strong>  [<strong>P</strong>]
  1152. </pre></div>
  1153.         </dd>
  1154.       </dl>
  1155.  
  1156.     
  1157.  
  1158.     <h3>Reverse Dynamic Mirror</h3>
  1159.  
  1160.       
  1161.  
  1162.       <dl>
  1163.         <dt>Description:</dt>
  1164.  
  1165.         <dd>...</dd>
  1166.  
  1167.         <dt>Solution:</dt>
  1168.  
  1169.         <dd>
  1170. <div class="example"><pre>
  1171. RewriteEngine on
  1172. RewriteCond   /mirror/of/remotesite/$1           -U
  1173. RewriteRule   ^http://www\.remotesite\.com/(.*)$ /mirror/of/remotesite/$1
  1174. </pre></div>
  1175.         </dd>
  1176.       </dl>
  1177.  
  1178.     
  1179.  
  1180.     <h3>Retrieve Missing Data from Intranet</h3>
  1181.  
  1182.       
  1183.  
  1184.       <dl>
  1185.         <dt>Description:</dt>
  1186.  
  1187.         <dd>
  1188.           <p>This is a tricky way of virtually running a corporate
  1189.           (external) Internet webserver
  1190.           (<code>www.quux-corp.dom</code>), while actually keeping
  1191.           and maintaining its data on a (internal) Intranet webserver
  1192.           (<code>www2.quux-corp.dom</code>) which is protected by a
  1193.           firewall. The trick is that on the external webserver we
  1194.           retrieve the requested data on-the-fly from the internal
  1195.           one.</p>
  1196.         </dd>
  1197.  
  1198.         <dt>Solution:</dt>
  1199.  
  1200.         <dd>
  1201.           <p>First, we have to make sure that our firewall still
  1202.           protects the internal webserver and that only the
  1203.           external webserver is allowed to retrieve data from it.
  1204.           For a packet-filtering firewall we could for instance
  1205.           configure a firewall ruleset like the following:</p>
  1206.  
  1207. <div class="example"><pre>
  1208. <strong>ALLOW</strong> Host www.quux-corp.dom Port >1024 --> Host www2.quux-corp.dom Port <strong>80</strong>
  1209. <strong>DENY</strong>  Host *                 Port *     --> Host www2.quux-corp.dom Port <strong>80</strong>
  1210. </pre></div>
  1211.  
  1212.           <p>Just adjust it to your actual configuration syntax.
  1213.           Now we can establish the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
  1214.           rules which request the missing data in the background
  1215.           through the proxy throughput feature:</p>
  1216.  
  1217. <div class="example"><pre>
  1218. RewriteRule ^/~([^/]+)/?(.*)          /home/$1/.www/$2
  1219. RewriteCond %{REQUEST_FILENAME}       <strong>!-f</strong>
  1220. RewriteCond %{REQUEST_FILENAME}       <strong>!-d</strong>
  1221. RewriteRule ^/home/([^/]+)/.www/?(.*) http://<strong>www2</strong>.quux-corp.dom/~$1/pub/$2 [<strong>P</strong>]
  1222. </pre></div>
  1223.         </dd>
  1224.       </dl>
  1225.  
  1226.     
  1227.  
  1228.     <h3>Load Balancing</h3>
  1229.  
  1230.       
  1231.  
  1232.       <dl>
  1233.         <dt>Description:</dt>
  1234.  
  1235.         <dd>
  1236.           <p>Suppose we want to load balance the traffic to
  1237.           <code>www.foo.com</code> over <code>www[0-5].foo.com</code>
  1238.           (a total of 6 servers). How can this be done?</p>
  1239.         </dd>
  1240.  
  1241.         <dt>Solution:</dt>
  1242.  
  1243.         <dd>
  1244.           <p>There are a lot of possible solutions for this problem.
  1245.           We will discuss first a commonly known DNS-based variant
  1246.           and then the special one with <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>:</p>
  1247.  
  1248.           <ol>
  1249.             <li>
  1250.               <strong>DNS Round-Robin</strong>
  1251.  
  1252.               <p>The simplest method for load-balancing is to use
  1253.               the DNS round-robin feature of <code>BIND</code>.
  1254.               Here you just configure <code>www[0-9].foo.com</code>
  1255.               as usual in your DNS with A(address) records, e.g.</p>
  1256.  
  1257. <div class="example"><pre>
  1258. www0   IN  A       1.2.3.1
  1259. www1   IN  A       1.2.3.2
  1260. www2   IN  A       1.2.3.3
  1261. www3   IN  A       1.2.3.4
  1262. www4   IN  A       1.2.3.5
  1263. www5   IN  A       1.2.3.6
  1264. </pre></div>
  1265.  
  1266.               <p>Then you additionally add the following entry:</p>
  1267.  
  1268. <div class="example"><pre>
  1269. www   IN  A       1.2.3.1
  1270. www   IN  A       1.2.3.2
  1271. www   IN  A       1.2.3.3
  1272. www   IN  A       1.2.3.4
  1273. www   IN  A       1.2.3.5
  1274. </pre></div>
  1275.  
  1276.               <p>Now when <code>www.foo.com</code> gets
  1277.               resolved, <code>BIND</code> gives out <code>www0-www5</code>
  1278.               - but in a slightly permutated/rotated order every time.
  1279.               This way the clients are spread over the various
  1280.               servers. But notice that this not a perfect load
  1281.               balancing scheme, because DNS resolve information
  1282.               gets cached by the other nameservers on the net, so
  1283.               once a client has resolved <code>www.foo.com</code>
  1284.               to a particular <code>wwwN.foo.com</code>, all
  1285.               subsequent requests also go to this particular name
  1286.               <code>wwwN.foo.com</code>. But the final result is
  1287.               ok, because the total sum of the requests are really
  1288.               spread over the various webservers.</p>
  1289.             </li>
  1290.  
  1291.             <li>
  1292.               <strong>DNS Load-Balancing</strong>
  1293.  
  1294.               <p>A sophisticated DNS-based method for
  1295.               load-balancing is to use the program
  1296.               <code>lbnamed</code> which can be found at <a href="http://www.stanford.edu/~schemers/docs/lbnamed/lbnamed.html">
  1297.               http://www.stanford.edu/~schemers/docs/lbnamed/lbnamed.html</a>.
  1298.               It is a Perl 5 program in conjunction with auxilliary
  1299.               tools which provides a real load-balancing for
  1300.               DNS.</p>
  1301.             </li>
  1302.  
  1303.             <li>
  1304.               <strong>Proxy Throughput Round-Robin</strong>
  1305.  
  1306.               <p>In this variant we use <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
  1307.               and its proxy throughput feature. First we dedicate
  1308.               <code>www0.foo.com</code> to be actually
  1309.               <code>www.foo.com</code> by using a single</p>
  1310.  
  1311. <div class="example"><pre>
  1312. www    IN  CNAME   www0.foo.com.
  1313. </pre></div>
  1314.  
  1315.               <p>entry in the DNS. Then we convert
  1316.               <code>www0.foo.com</code> to a proxy-only server,
  1317.               i.e. we configure this machine so all arriving URLs
  1318.               are just pushed through the internal proxy to one of
  1319.               the 5 other servers (<code>www1-www5</code>). To
  1320.               accomplish this we first establish a ruleset which
  1321.               contacts a load balancing script <code>lb.pl</code>
  1322.               for all URLs.</p>
  1323.  
  1324. <div class="example"><pre>
  1325. RewriteEngine on
  1326. RewriteMap    lb      prg:/path/to/lb.pl
  1327. RewriteRule   ^/(.+)$ ${lb:$1}           [P,L]
  1328. </pre></div>
  1329.  
  1330.               <p>Then we write <code>lb.pl</code>:</p>
  1331.  
  1332. <div class="example"><pre>
  1333. #!/path/to/perl
  1334. ##
  1335. ##  lb.pl -- load balancing script
  1336. ##
  1337.  
  1338. $| = 1;
  1339.  
  1340. $name   = "www";     # the hostname base
  1341. $first  = 1;         # the first server (not 0 here, because 0 is myself)
  1342. $last   = 5;         # the last server in the round-robin
  1343. $domain = "foo.dom"; # the domainname
  1344.  
  1345. $cnt = 0;
  1346. while (<STDIN>) {
  1347.     $cnt = (($cnt+1) % ($last+1-$first));
  1348.     $server = sprintf("%s%d.%s", $name, $cnt+$first, $domain);
  1349.     print "http://$server/$_";
  1350. }
  1351.  
  1352. ##EOF##
  1353. </pre></div>
  1354.  
  1355.               <div class="note">A last notice: Why is this useful? Seems like
  1356.               <code>www0.foo.com</code> still is overloaded? The
  1357.               answer is yes, it is overloaded, but with plain proxy
  1358.               throughput requests, only! All SSI, CGI, ePerl, etc.
  1359.               processing is completely done on the other machines.
  1360.               This is the essential point.</div>
  1361.             </li>
  1362.  
  1363.             <li>
  1364.               <strong>Hardware/TCP Round-Robin</strong>
  1365.  
  1366.               <p>There is a hardware solution available, too. Cisco
  1367.               has a beast called LocalDirector which does a load
  1368.               balancing at the TCP/IP level. Actually this is some
  1369.               sort of a circuit level gateway in front of a
  1370.               webcluster. If you have enough money and really need
  1371.               a solution with high performance, use this one.</p>
  1372.             </li>
  1373.           </ol>
  1374.         </dd>
  1375.       </dl>
  1376.  
  1377.     
  1378.  
  1379.     <h3>New MIME-type, New Service</h3>
  1380.  
  1381.       
  1382.  
  1383.       <dl>
  1384.         <dt>Description:</dt>
  1385.  
  1386.         <dd>
  1387.           <p>On the net there are a lot of nifty CGI programs. But
  1388.           their usage is usually boring, so a lot of webmaster
  1389.           don't use them. Even Apache's Action handler feature for
  1390.           MIME-types is only appropriate when the CGI programs
  1391.           don't need special URLs (actually <code>PATH_INFO</code>
  1392.           and <code>QUERY_STRINGS</code>) as their input. First,
  1393.           let us configure a new file type with extension
  1394.           <code>.scgi</code> (for secure CGI) which will be processed
  1395.           by the popular <code>cgiwrap</code> program. The problem
  1396.           here is that for instance we use a Homogeneous URL Layout
  1397.           (see above) a file inside the user homedirs has the URL
  1398.           <code>/u/user/foo/bar.scgi</code>. But
  1399.           <code>cgiwrap</code> needs the URL in the form
  1400.           <code>/~user/foo/bar.scgi/</code>. The following rule
  1401.           solves the problem:</p>
  1402.  
  1403. <div class="example"><pre>
  1404. RewriteRule ^/[uge]/<strong>([^/]+)</strong>/\.www/(.+)\.scgi(.*) ...
  1405. ... /internal/cgi/user/cgiwrap/~<strong>$1</strong>/$2.scgi$3  [NS,<strong>T=application/x-http-cgi</strong>]
  1406. </pre></div>
  1407.  
  1408.           <p>Or assume we have some more nifty programs:
  1409.           <code>wwwlog</code> (which displays the
  1410.           <code>access.log</code> for a URL subtree and
  1411.           <code>wwwidx</code> (which runs Glimpse on a URL
  1412.           subtree). We have to provide the URL area to these
  1413.           programs so they know on which area they have to act on.
  1414.           But usually this ugly, because they are all the times
  1415.           still requested from that areas, i.e. typically we would
  1416.           run the <code>swwidx</code> program from within
  1417.           <code>/u/user/foo/</code> via hyperlink to</p>
  1418.  
  1419. <div class="example"><pre>
  1420. /internal/cgi/user/swwidx?i=/u/user/foo/
  1421. </pre></div>
  1422.  
  1423.           <p>which is ugly. Because we have to hard-code
  1424.           <strong>both</strong> the location of the area
  1425.           <strong>and</strong> the location of the CGI inside the
  1426.           hyperlink. When we have to reorganize the area, we spend a
  1427.           lot of time changing the various hyperlinks.</p>
  1428.         </dd>
  1429.  
  1430.         <dt>Solution:</dt>
  1431.  
  1432.         <dd>
  1433.           <p>The solution here is to provide a special new URL format
  1434.           which automatically leads to the proper CGI invocation.
  1435.           We configure the following:</p>
  1436.  
  1437. <div class="example"><pre>
  1438. RewriteRule   ^/([uge])/([^/]+)(/?.*)/\*  /internal/cgi/user/wwwidx?i=/$1/$2$3/
  1439. RewriteRule   ^/([uge])/([^/]+)(/?.*):log /internal/cgi/user/wwwlog?f=/$1/$2$3
  1440. </pre></div>
  1441.  
  1442.           <p>Now the hyperlink to search at
  1443.           <code>/u/user/foo/</code> reads only</p>
  1444.  
  1445. <div class="example"><pre>
  1446. HREF="*"
  1447. </pre></div>
  1448.  
  1449.           <p>which internally gets automatically transformed to</p>
  1450.  
  1451. <div class="example"><pre>
  1452. /internal/cgi/user/wwwidx?i=/u/user/foo/
  1453. </pre></div>
  1454.  
  1455.           <p>The same approach leads to an invocation for the
  1456.           access log CGI program when the hyperlink
  1457.           <code>:log</code> gets used.</p>
  1458.         </dd>
  1459.       </dl>
  1460.  
  1461.     
  1462.  
  1463.     <h3>From Static to Dynamic</h3>
  1464.  
  1465.       
  1466.  
  1467.       <dl>
  1468.         <dt>Description:</dt>
  1469.  
  1470.         <dd>
  1471.           <p>How can we transform a static page
  1472.           <code>foo.html</code> into a dynamic variant
  1473.           <code>foo.cgi</code> in a seamless way, i.e. without notice
  1474.           by the browser/user.</p>
  1475.         </dd>
  1476.  
  1477.         <dt>Solution:</dt>
  1478.  
  1479.         <dd>
  1480.           <p>We just rewrite the URL to the CGI-script and force the
  1481.           correct MIME-type so it gets really run as a CGI-script.
  1482.           This way a request to <code>/~quux/foo.html</code>
  1483.           internally leads to the invocation of
  1484.           <code>/~quux/foo.cgi</code>.</p>
  1485.  
  1486. <div class="example"><pre>
  1487. RewriteEngine  on
  1488. RewriteBase    /~quux/
  1489. RewriteRule    ^foo\.<strong>html</strong>$  foo.<strong>cgi</strong>  [T=<strong>application/x-httpd-cgi</strong>]
  1490. </pre></div>
  1491.         </dd>
  1492.       </dl>
  1493.  
  1494.     
  1495.  
  1496.     <h3>On-the-fly Content-Regeneration</h3>
  1497.  
  1498.       
  1499.  
  1500.       <dl>
  1501.         <dt>Description:</dt>
  1502.  
  1503.         <dd>
  1504.           <p>Here comes a really esoteric feature: Dynamically
  1505.           generated but statically served pages, i.e. pages should be
  1506.           delivered as pure static pages (read from the filesystem
  1507.           and just passed through), but they have to be generated
  1508.           dynamically by the webserver if missing. This way you can
  1509.           have CGI-generated pages which are statically served unless
  1510.           one (or a cronjob) removes the static contents. Then the
  1511.           contents gets refreshed.</p>
  1512.         </dd>
  1513.  
  1514.         <dt>Solution:</dt>
  1515.  
  1516.         <dd>
  1517.           This is done via the following ruleset:
  1518.  
  1519. <div class="example"><pre>
  1520. RewriteCond %{REQUEST_FILENAME}   <strong>!-s</strong>
  1521. RewriteRule ^page\.<strong>html</strong>$          page.<strong>cgi</strong>   [T=application/x-httpd-cgi,L]
  1522. </pre></div>
  1523.  
  1524.           <p>Here a request to <code>page.html</code> leads to a
  1525.           internal run of a corresponding <code>page.cgi</code> if
  1526.           <code>page.html</code> is still missing or has filesize
  1527.           null. The trick here is that <code>page.cgi</code> is a
  1528.           usual CGI script which (additionally to its <code>STDOUT</code>)
  1529.           writes its output to the file <code>page.html</code>.
  1530.           Once it was run, the server sends out the data of
  1531.           <code>page.html</code>. When the webmaster wants to force
  1532.           a refresh the contents, he just removes
  1533.           <code>page.html</code> (usually done by a cronjob).</p>
  1534.         </dd>
  1535.       </dl>
  1536.  
  1537.     
  1538.  
  1539.     <h3>Document With Autorefresh</h3>
  1540.  
  1541.       
  1542.  
  1543.       <dl>
  1544.         <dt>Description:</dt>
  1545.  
  1546.         <dd>
  1547.           <p>Wouldn't it be nice while creating a complex webpage if
  1548.           the webbrowser would automatically refresh the page every
  1549.           time we write a new version from within our editor?
  1550.           Impossible?</p>
  1551.         </dd>
  1552.  
  1553.         <dt>Solution:</dt>
  1554.  
  1555.         <dd>
  1556.           <p>No! We just combine the MIME multipart feature, the
  1557.           webserver NPH feature and the URL manipulation power of
  1558.           <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>. First, we establish a new
  1559.           URL feature: Adding just <code>:refresh</code> to any
  1560.           URL causes this to be refreshed every time it gets
  1561.           updated on the filesystem.</p>
  1562.  
  1563. <div class="example"><pre>
  1564. RewriteRule   ^(/[uge]/[^/]+/?.*):refresh  /internal/cgi/apache/nph-refresh?f=$1
  1565. </pre></div>
  1566.  
  1567.           <p>Now when we reference the URL</p>
  1568.  
  1569. <div class="example"><pre>
  1570. /u/foo/bar/page.html:refresh
  1571. </pre></div>
  1572.  
  1573.           <p>this leads to the internal invocation of the URL</p>
  1574.  
  1575. <div class="example"><pre>
  1576. /internal/cgi/apache/nph-refresh?f=/u/foo/bar/page.html
  1577. </pre></div>
  1578.  
  1579.           <p>The only missing part is the NPH-CGI script. Although
  1580.           one would usually say "left as an exercise to the reader"
  1581.           ;-) I will provide this, too.</p>
  1582.  
  1583. <div class="example"><pre>
  1584. #!/sw/bin/perl
  1585. ##
  1586. ##  nph-refresh -- NPH/CGI script for auto refreshing pages
  1587. ##  Copyright (c) 1997 Ralf S. Engelschall, All Rights Reserved.
  1588. ##
  1589. $| = 1;
  1590.  
  1591. #   split the QUERY_STRING variable
  1592. @pairs = split(/&/, $ENV{'QUERY_STRING'});
  1593. foreach $pair (@pairs) {
  1594.     ($name, $value) = split(/=/, $pair);
  1595.     $name =~ tr/A-Z/a-z/;
  1596.     $name = 'QS_' . $name;
  1597.     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  1598.     eval "\$$name = \"$value\"";
  1599. }
  1600. $QS_s = 1 if ($QS_s eq '');
  1601. $QS_n = 3600 if ($QS_n eq '');
  1602. if ($QS_f eq '') {
  1603.     print "HTTP/1.0 200 OK\n";
  1604.     print "Content-type: text/html\n\n";
  1605.     print "&lt;b&gt;ERROR&lt;/b&gt;: No file given\n";
  1606.     exit(0);
  1607. }
  1608. if (! -f $QS_f) {
  1609.     print "HTTP/1.0 200 OK\n";
  1610.     print "Content-type: text/html\n\n";
  1611.     print "&lt;b&gt;ERROR&lt;/b&gt;: File $QS_f not found\n";
  1612.     exit(0);
  1613. }
  1614.  
  1615. sub print_http_headers_multipart_begin {
  1616.     print "HTTP/1.0 200 OK\n";
  1617.     $bound = "ThisRandomString12345";
  1618.     print "Content-type: multipart/x-mixed-replace;boundary=$bound\n";
  1619.     &print_http_headers_multipart_next;
  1620. }
  1621.  
  1622. sub print_http_headers_multipart_next {
  1623.     print "\n--$bound\n";
  1624. }
  1625.  
  1626. sub print_http_headers_multipart_end {
  1627.     print "\n--$bound--\n";
  1628. }
  1629.  
  1630. sub displayhtml {
  1631.     local($buffer) = @_;
  1632.     $len = length($buffer);
  1633.     print "Content-type: text/html\n";
  1634.     print "Content-length: $len\n\n";
  1635.     print $buffer;
  1636. }
  1637.  
  1638. sub readfile {
  1639.     local($file) = @_;
  1640.     local(*FP, $size, $buffer, $bytes);
  1641.     ($x, $x, $x, $x, $x, $x, $x, $size) = stat($file);
  1642.     $size = sprintf("%d", $size);
  1643.     open(FP, "&lt;$file");
  1644.     $bytes = sysread(FP, $buffer, $size);
  1645.     close(FP);
  1646.     return $buffer;
  1647. }
  1648.  
  1649. $buffer = &readfile($QS_f);
  1650. &print_http_headers_multipart_begin;
  1651. &displayhtml($buffer);
  1652.  
  1653. sub mystat {
  1654.     local($file) = $_[0];
  1655.     local($time);
  1656.  
  1657.     ($x, $x, $x, $x, $x, $x, $x, $x, $x, $mtime) = stat($file);
  1658.     return $mtime;
  1659. }
  1660.  
  1661. $mtimeL = &mystat($QS_f);
  1662. $mtime = $mtime;
  1663. for ($n = 0; $n &lt; $QS_n; $n++) {
  1664.     while (1) {
  1665.         $mtime = &mystat($QS_f);
  1666.         if ($mtime ne $mtimeL) {
  1667.             $mtimeL = $mtime;
  1668.             sleep(2);
  1669.             $buffer = &readfile($QS_f);
  1670.             &print_http_headers_multipart_next;
  1671.             &displayhtml($buffer);
  1672.             sleep(5);
  1673.             $mtimeL = &mystat($QS_f);
  1674.             last;
  1675.         }
  1676.         sleep($QS_s);
  1677.     }
  1678. }
  1679.  
  1680. &print_http_headers_multipart_end;
  1681.  
  1682. exit(0);
  1683.  
  1684. ##EOF##
  1685. </pre></div>
  1686.         </dd>
  1687.       </dl>
  1688.  
  1689.     
  1690.  
  1691.     <h3>Mass Virtual Hosting</h3>
  1692.  
  1693.       
  1694.  
  1695.       <dl>
  1696.         <dt>Description:</dt>
  1697.  
  1698.         <dd>
  1699.           <p>The <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> feature of Apache is nice
  1700.           and works great when you just have a few dozens
  1701.           virtual hosts. But when you are an ISP and have hundreds of
  1702.           virtual hosts to provide this feature is not the best
  1703.           choice.</p>
  1704.         </dd>
  1705.  
  1706.         <dt>Solution:</dt>
  1707.  
  1708.         <dd>
  1709.           <p>To provide this feature we map the remote webpage or even
  1710.           the complete remote webarea to our namespace by the use
  1711.           of the <dfn>Proxy Throughput</dfn> feature (flag <code>[P]</code>):</p>
  1712.  
  1713. <div class="example"><pre>
  1714. ##
  1715. ##  vhost.map
  1716. ##
  1717. www.vhost1.dom:80  /path/to/docroot/vhost1
  1718. www.vhost2.dom:80  /path/to/docroot/vhost2
  1719.      :
  1720. www.vhostN.dom:80  /path/to/docroot/vhostN
  1721. </pre></div>
  1722.  
  1723. <div class="example"><pre>
  1724. ##
  1725. ##  httpd.conf
  1726. ##
  1727.     :
  1728. #   use the canonical hostname on redirects, etc.
  1729. UseCanonicalName on
  1730.  
  1731.     :
  1732. #   add the virtual host in front of the CLF-format
  1733. CustomLog  /path/to/access_log  "%{VHOST}e %h %l %u %t \"%r\" %>s %b"
  1734.     :
  1735.  
  1736. #   enable the rewriting engine in the main server
  1737. RewriteEngine on
  1738.  
  1739. #   define two maps: one for fixing the URL and one which defines
  1740. #   the available virtual hosts with their corresponding
  1741. #   DocumentRoot.
  1742. RewriteMap    lowercase    int:tolower
  1743. RewriteMap    vhost        txt:/path/to/vhost.map
  1744.  
  1745. #   Now do the actual virtual host mapping
  1746. #   via a huge and complicated single rule:
  1747. #
  1748. #   1. make sure we don't map for common locations
  1749. RewriteCond   %{REQUEST_URL}  !^/commonurl1/.*
  1750. RewriteCond   %{REQUEST_URL}  !^/commonurl2/.*
  1751.     :
  1752. RewriteCond   %{REQUEST_URL}  !^/commonurlN/.*
  1753. #
  1754. #   2. make sure we have a Host header, because
  1755. #      currently our approach only supports
  1756. #      virtual hosting through this header
  1757. RewriteCond   %{HTTP_HOST}  !^$
  1758. #
  1759. #   3. lowercase the hostname
  1760. RewriteCond   ${lowercase:%{HTTP_HOST}|NONE}  ^(.+)$
  1761. #
  1762. #   4. lookup this hostname in vhost.map and
  1763. #      remember it only when it is a path
  1764. #      (and not "NONE" from above)
  1765. RewriteCond   ${vhost:%1}  ^(/.*)$
  1766. #
  1767. #   5. finally we can map the URL to its docroot location
  1768. #      and remember the virtual host for logging puposes
  1769. RewriteRule   ^/(.*)$   %1/$1  [E=VHOST:${lowercase:%{HTTP_HOST}}]
  1770.     :
  1771. </pre></div>
  1772.         </dd>
  1773.       </dl>
  1774.  
  1775.     
  1776.  
  1777.   </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  1778. <div class="section">
  1779. <h2><a name="access" id="access">Access Restriction</a></h2>
  1780.  
  1781.     
  1782.  
  1783.     <h3>Blocking of Robots</h3>
  1784.  
  1785.       
  1786.  
  1787.       <dl>
  1788.         <dt>Description:</dt>
  1789.  
  1790.         <dd>
  1791.           <p>How can we block a really annoying robot from
  1792.           retrieving pages of a specific webarea? A
  1793.           <code>/robots.txt</code> file containing entries of the
  1794.           "Robot Exclusion Protocol" is typically not enough to get
  1795.           rid of such a robot.</p>
  1796.         </dd>
  1797.  
  1798.         <dt>Solution:</dt>
  1799.  
  1800.         <dd>
  1801.           <p>We use a ruleset which forbids the URLs of the webarea
  1802.           <code>/~quux/foo/arc/</code> (perhaps a very deep
  1803.           directory indexed area where the robot traversal would
  1804.           create big server load). We have to make sure that we
  1805.           forbid access only to the particular robot, i.e. just
  1806.           forbidding the host where the robot runs is not enough.
  1807.           This would block users from this host, too. We accomplish
  1808.           this by also matching the User-Agent HTTP header
  1809.           information.</p>
  1810.  
  1811. <div class="example"><pre>
  1812. RewriteCond %{HTTP_USER_AGENT}   ^<strong>NameOfBadRobot</strong>.*
  1813. RewriteCond %{REMOTE_ADDR}       ^<strong>123\.45\.67\.[8-9]</strong>$
  1814. RewriteRule ^<strong>/~quux/foo/arc/</strong>.+   -   [<strong>F</strong>]
  1815. </pre></div>
  1816.         </dd>
  1817.       </dl>
  1818.  
  1819.     
  1820.  
  1821.     <h3>Blocked Inline-Images</h3>
  1822.  
  1823.       
  1824.  
  1825.       <dl>
  1826.         <dt>Description:</dt>
  1827.  
  1828.         <dd>
  1829.           <p>Assume we have under <code>http://www.quux-corp.de/~quux/</code>
  1830.           some pages with inlined GIF graphics. These graphics are
  1831.           nice, so others directly incorporate them via hyperlinks to
  1832.           their pages. We don't like this practice because it adds
  1833.           useless traffic to our server.</p>
  1834.         </dd>
  1835.  
  1836.         <dt>Solution:</dt>
  1837.  
  1838.         <dd>
  1839.           <p>While we cannot 100% protect the images from inclusion,
  1840.           we can at least restrict the cases where the browser
  1841.           sends a HTTP Referer header.</p>
  1842.  
  1843. <div class="example"><pre>
  1844. RewriteCond %{HTTP_REFERER} <strong>!^$</strong>
  1845. RewriteCond %{HTTP_REFERER} !^http://www.quux-corp.de/~quux/.*$ [NC]
  1846. RewriteRule <strong>.*\.gif$</strong>        -                                    [F]
  1847. </pre></div>
  1848.  
  1849. <div class="example"><pre>
  1850. RewriteCond %{HTTP_REFERER}         !^$
  1851. RewriteCond %{HTTP_REFERER}         !.*/foo-with-gif\.html$
  1852. RewriteRule <strong>^inlined-in-foo\.gif$</strong>   -                        [F]
  1853. </pre></div>
  1854.         </dd>
  1855.       </dl>
  1856.  
  1857.     
  1858.  
  1859.     <h3>Host Deny</h3>
  1860.  
  1861.       
  1862.  
  1863.       <dl>
  1864.         <dt>Description:</dt>
  1865.  
  1866.         <dd>
  1867.           <p>How can we forbid a list of externally configured hosts
  1868.           from using our server?</p>
  1869.         </dd>
  1870.  
  1871.         <dt>Solution:</dt>
  1872.  
  1873.         <dd>
  1874.           <p>For Apache >= 1.3b6:</p>
  1875.  
  1876. <div class="example"><pre>
  1877. RewriteEngine on
  1878. RewriteMap    hosts-deny  txt:/path/to/hosts.deny
  1879. RewriteCond   ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND [OR]
  1880. RewriteCond   ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND
  1881. RewriteRule   ^/.*  -  [F]
  1882. </pre></div>
  1883.  
  1884.           <p>For Apache <= 1.3b6:</p>
  1885.  
  1886. <div class="example"><pre>
  1887. RewriteEngine on
  1888. RewriteMap    hosts-deny  txt:/path/to/hosts.deny
  1889. RewriteRule   ^/(.*)$ ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND}/$1
  1890. RewriteRule   !^NOT-FOUND/.* - [F]
  1891. RewriteRule   ^NOT-FOUND/(.*)$ ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND}/$1
  1892. RewriteRule   !^NOT-FOUND/.* - [F]
  1893. RewriteRule   ^NOT-FOUND/(.*)$ /$1
  1894. </pre></div>
  1895.  
  1896. <div class="example"><pre>
  1897. ##
  1898. ##  hosts.deny
  1899. ##
  1900. ##  ATTENTION! This is a map, not a list, even when we treat it as such.
  1901. ##             mod_rewrite parses it for key/value pairs, so at least a
  1902. ##             dummy value "-" must be present for each entry.
  1903. ##
  1904.  
  1905. 193.102.180.41 -
  1906. bsdti1.sdm.de  -
  1907. 192.76.162.40  -
  1908. </pre></div>
  1909.         </dd>
  1910.       </dl>
  1911.  
  1912.     
  1913.  
  1914.     <h3>Proxy Deny</h3>
  1915.  
  1916.       
  1917.  
  1918.       <dl>
  1919.         <dt>Description:</dt>
  1920.  
  1921.         <dd>
  1922.           <p>How can we forbid a certain host or even a user of a
  1923.           special host from using the Apache proxy?</p>
  1924.         </dd>
  1925.  
  1926.         <dt>Solution:</dt>
  1927.  
  1928.         <dd>
  1929.           <p>We first have to make sure <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
  1930.           is below(!) <code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code> in the Configuration
  1931.           file when compiling the Apache webserver. This way it gets
  1932.           called <em>before</em> <code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code>. Then we
  1933.           configure the following for a host-dependent deny...</p>
  1934.  
  1935. <div class="example"><pre>
  1936. RewriteCond %{REMOTE_HOST} <strong>^badhost\.mydomain\.com$</strong>
  1937. RewriteRule !^http://[^/.]\.mydomain.com.*  - [F]
  1938. </pre></div>
  1939.  
  1940.           <p>...and this one for a user@host-dependent deny:</p>
  1941.  
  1942. <div class="example"><pre>
  1943. RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST}  <strong>^badguy@badhost\.mydomain\.com$</strong>
  1944. RewriteRule !^http://[^/.]\.mydomain.com.*  - [F]
  1945. </pre></div>
  1946.         </dd>
  1947.       </dl>
  1948.  
  1949.     
  1950.  
  1951.     <h3>Special Authentication Variant</h3>
  1952.  
  1953.       
  1954.  
  1955.       <dl>
  1956.         <dt>Description:</dt>
  1957.  
  1958.         <dd>
  1959.           <p>Sometimes a very special authentication is needed, for
  1960.           instance a authentication which checks for a set of
  1961.           explicitly configured users. Only these should receive
  1962.           access and without explicit prompting (which would occur
  1963.           when using the Basic Auth via <code class="module"><a href="../mod/mod_auth_basic.html">mod_auth_basic</a></code>).</p>
  1964.         </dd>
  1965.  
  1966.         <dt>Solution:</dt>
  1967.  
  1968.         <dd>
  1969.           <p>We use a list of rewrite conditions to exclude all except
  1970.           our friends:</p>
  1971.  
  1972. <div class="example"><pre>
  1973. RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} <strong>!^friend1@client1.quux-corp\.com$</strong>
  1974. RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} <strong>!^friend2</strong>@client2.quux-corp\.com$
  1975. RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} <strong>!^friend3</strong>@client3.quux-corp\.com$
  1976. RewriteRule ^/~quux/only-for-friends/      -                                 [F]
  1977. </pre></div>
  1978.         </dd>
  1979.       </dl>
  1980.  
  1981.     
  1982.  
  1983.     <h3>Referer-based Deflector</h3>
  1984.  
  1985.       
  1986.  
  1987.       <dl>
  1988.         <dt>Description:</dt>
  1989.  
  1990.         <dd>
  1991.           <p>How can we program a flexible URL Deflector which acts
  1992.           on the "Referer" HTTP header and can be configured with as
  1993.           many referring pages as we like?</p>
  1994.         </dd>
  1995.  
  1996.         <dt>Solution:</dt>
  1997.  
  1998.         <dd>
  1999.           <p>Use the following really tricky ruleset...</p>
  2000.  
  2001. <div class="example"><pre>
  2002. RewriteMap  deflector txt:/path/to/deflector.map
  2003.  
  2004. RewriteCond %{HTTP_REFERER} !=""
  2005. RewriteCond ${deflector:%{HTTP_REFERER}} ^-$
  2006. RewriteRule ^.* %{HTTP_REFERER} [R,L]
  2007.  
  2008. RewriteCond %{HTTP_REFERER} !=""
  2009. RewriteCond ${deflector:%{HTTP_REFERER}|NOT-FOUND} !=NOT-FOUND
  2010. RewriteRule ^.* ${deflector:%{HTTP_REFERER}} [R,L]
  2011. </pre></div>
  2012.  
  2013.           <p>... in conjunction with a corresponding rewrite
  2014.           map:</p>
  2015.  
  2016. <div class="example"><pre>
  2017. ##
  2018. ##  deflector.map
  2019. ##
  2020.  
  2021. http://www.badguys.com/bad/index.html    -
  2022. http://www.badguys.com/bad/index2.html   -
  2023. http://www.badguys.com/bad/index3.html   http://somewhere.com/
  2024. </pre></div>
  2025.  
  2026.           <p>This automatically redirects the request back to the
  2027.           referring page (when "<code>-</code>" is used as the value
  2028.           in the map) or to a specific URL (when an URL is specified
  2029.           in the map as the second argument).</p>
  2030.         </dd>
  2031.       </dl>
  2032.  
  2033.     
  2034.  
  2035.   </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  2036. <div class="section">
  2037. <h2><a name="other" id="other">Other</a></h2>
  2038.  
  2039.     
  2040.  
  2041.     <h3>External Rewriting Engine</h3>
  2042.  
  2043.       
  2044.  
  2045.       <dl>
  2046.         <dt>Description:</dt>
  2047.  
  2048.         <dd>
  2049.           <p>A FAQ: How can we solve the FOO/BAR/QUUX/etc.
  2050.           problem? There seems no solution by the use of
  2051.           <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>...</p>
  2052.         </dd>
  2053.  
  2054.         <dt>Solution:</dt>
  2055.  
  2056.         <dd>
  2057.           <p>Use an external <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>, i.e. a program which acts
  2058.           like a <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>. It is run once on startup of Apache
  2059.           receives the requested URLs on <code>STDIN</code> and has
  2060.           to put the resulting (usually rewritten) URL on
  2061.           <code>STDOUT</code> (same order!).</p>
  2062.  
  2063. <div class="example"><pre>
  2064. RewriteEngine on
  2065. RewriteMap    quux-map       <strong>prg:</strong>/path/to/map.quux.pl
  2066. RewriteRule   ^/~quux/(.*)$  /~quux/<strong>${quux-map:$1}</strong>
  2067. </pre></div>
  2068.  
  2069. <div class="example"><pre>
  2070. #!/path/to/perl
  2071.  
  2072. #   disable buffered I/O which would lead
  2073. #   to deadloops for the Apache server
  2074. $| = 1;
  2075.  
  2076. #   read URLs one per line from stdin and
  2077. #   generate substitution URL on stdout
  2078. while (<>) {
  2079.     s|^foo/|bar/|;
  2080.     print $_;
  2081. }
  2082. </pre></div>
  2083.  
  2084.           <p>This is a demonstration-only example and just rewrites
  2085.           all URLs <code>/~quux/foo/...</code> to
  2086.           <code>/~quux/bar/...</code>. Actually you can program
  2087.           whatever you like. But notice that while such maps can be
  2088.           <strong>used</strong> also by an average user, only the
  2089.           system administrator can <strong>define</strong> it.</p>
  2090.         </dd>
  2091.       </dl>
  2092.  
  2093.     
  2094.  
  2095.   </div></div>
  2096. <div class="bottomlang">
  2097. <p><span>Available Languages: </span><a href="../en/misc/rewriteguide.html" title="English"> en </a> |
  2098. <a href="../ko/misc/rewriteguide.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
  2099. </div><div id="footer">
  2100. <p class="apache">Copyright 2007 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>
  2101. <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>
  2102. </body></html>