home *** CD-ROM | disk | FTP | other *** search
/ PC World 2004 May / PCWorld_2004-05_cd.bin / komunikace / apache / apache_2.0.48-win32-x86-no_ssl.msi / Data.Cab / F252415_rewriteguide.xml < prev    next >
Extensible Markup Language  |  2003-07-07  |  69KB  |  2,085 lines

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