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