rewrite_guide.html.en revision 43a906741e1d11f0f1d0c189d1b55eafbcc21d14
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<?xml version="1.0" encoding="ISO-8859-1"?>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
af4381d113faafc97340eaaa008840c7fcbcc8fdsf XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
af4381d113faafc97340eaaa008840c7fcbcc8fdsf This file is generated from xml source: DO NOT EDIT
af4381d113faafc97340eaaa008840c7fcbcc8fdsf XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
af4381d113faafc97340eaaa008840c7fcbcc8fdsf -->
96ad5d81ee4a2cc66a4ae19893efc8aa6d06fae7jailletc<title>URL Rewriting Guide - Apache HTTP Server</title>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen<link href="/style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
2e545ce2450a9953665f701bb05350f0d3f26275nd<link href="/images/favicon.ico" rel="shortcut icon" /></head>
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen<body id="manual-page"><div id="page-header">
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen<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>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<p class="apache">Apache HTTP Server Version 2.3</p>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<img alt="" src="/images/feather.gif" /></div>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="/images/left.gif" /></a></div>
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen<div id="path">
3f08db06526d6901aa08c110b5bc7dde6bc39905nd<a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.3</a> &gt; <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>URL Rewriting Guide</h1>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<div class="toplang">
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<p><span>Available Languages: </span><a href="/en/rewrite/rewrite_guide.html" title="English">&nbsp;en&nbsp;</a></p>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf</div>
3f08db06526d6901aa08c110b5bc7dde6bc39905nd
af4381d113faafc97340eaaa008840c7fcbcc8fdsf
af4381d113faafc97340eaaa008840c7fcbcc8fdsf <p>This document supplements the <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf <a href="/mod/mod_rewrite.html">reference documentation</a>.
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung It describes how one can use Apache's <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf to solve typical URL-based problems with which webmasters are
af4381d113faafc97340eaaa008840c7fcbcc8fdsf commonly confronted. We give detailed descriptions on how to
af4381d113faafc97340eaaa008840c7fcbcc8fdsf solve each problem by configuring URL rewriting rulesets.</p>
7f0952c0239ea2d6e37b472db6fde4ef2718343dsf
af4381d113faafc97340eaaa008840c7fcbcc8fdsf <div class="warning">ATTENTION: Depending on your server configuration
b09fcdfc59ada4712150e7bcc7b502bb9e4601d8rjung it may be necessary to slightly change the examples for your
af4381d113faafc97340eaaa008840c7fcbcc8fdsf situation, e.g. adding the <code>[PT]</code> flag when
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri additionally using <code class="module"><a href="/mod/mod_alias.html">mod_alias</a></code> and
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri <code class="module"><a href="/mod/mod_userdir.html">mod_userdir</a></code>, etc. Or rewriting a ruleset
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri to fit in <code>.htaccess</code> context instead
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri of per-server context. Always try to understand what a
af4381d113faafc97340eaaa008840c7fcbcc8fdsf particular ruleset really does before you use it. This
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf avoids many problems.</div>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri </div>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#canonicalurl">Canonical URLs</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#canonicalhost">Canonical Hostnames</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#moveddocroot">Moved <code>DocumentRoot</code></a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#trailingslash">Trailing Slash Problem</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#movehomedirs">Move Homedirs to Different Webserver</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#multipledirs">Search for pages in more than one directory</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#setenvvars">Set Environment Variables According To URL Parts</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#uservhosts">Virtual Hosts Per User</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#redirecthome">Redirect Homedirs For Foreigners</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#redirectanchors">Redirecting Anchors</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#time-dependent">Time-Dependent Rewriting</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#backward-compatibility">Backward Compatibility for YYYY to XXXX migration</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#old-to-new">From Old to New (intern)</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#old-to-new-extern">From Old to New (extern)</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#static-to-dynamic">From Static to Dynamic</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#blocking-of-robots">Blocking of Robots</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#blocked-inline-images">Forbidding Image "Hotlinking"</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#proxy-deny">Proxy Deny</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<li><img alt="" src="/images/down.gif" /> <a href="#external-rewriting">External Rewriting Engine</a></li>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar</ul><h3>See also</h3><ul class="seealso"><li><a href="/mod/mod_rewrite.html">Module
1f1b6bf13313fdd14a45e52e553d3ff28689b717coardocumentation</a></li><li><a href="rewrite_intro.html">mod_rewrite
1f1b6bf13313fdd14a45e52e553d3ff28689b717coarintroduction</a></li><li><a href="rewrite_guide_advanced.html">Advanced Rewrite Guide - advanced
1f1b6bf13313fdd14a45e52e553d3ff28689b717coaruseful examples</a></li><li><a href="rewrite_tech.html">Technical details</a></li></ul></div>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<div class="section">
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<h2><a name="canonicalurl" id="canonicalurl">Canonical URLs</a></h2>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar<dl>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar <dt>Description:</dt>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar <dd>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar <p>On some webservers there are more than one URL for a
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar resource. Usually there are canonical URLs (which should be
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar actually used and distributed) and those which are just
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar shortcuts, internal ones, etc. Independent of which URL the
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar user supplied with the request he should finally see the
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar canonical one only.</p>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar </dd>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar <dt>Solution:</dt>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar <dd>
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar <p>We do an external HTTP redirect for all non-canonical
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar URLs to fix them in the location view of the Browser and
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf for all subsequent requests. In the example ruleset below
af4381d113faafc97340eaaa008840c7fcbcc8fdsf we replace <code>/~user</code> by the canonical
af4381d113faafc97340eaaa008840c7fcbcc8fdsf <code>/u/user</code> and fix a missing trailing slash for
af4381d113faafc97340eaaa008840c7fcbcc8fdsf <code>/u/user</code>.</p>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf
16a0ba19b2cecf27e48c0c197ae1f3a96f447949sf<div class="example"><pre>
16a0ba19b2cecf27e48c0c197ae1f3a96f447949sfRewriteRule ^/<strong>~</strong>([^/]+)/?(.*) /<strong>u</strong>/$1/$2 [<strong>R</strong>]
af4381d113faafc97340eaaa008840c7fcbcc8fdsfRewriteRule ^/u/(<strong>[^/]+</strong>)$ /$1/$2<strong>/</strong> [<strong>R</strong>]
16a0ba19b2cecf27e48c0c197ae1f3a96f447949sf</pre></div>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf </dd>
7f0952c0239ea2d6e37b472db6fde4ef2718343dsf </dl>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf
b09fcdfc59ada4712150e7bcc7b502bb9e4601d8rjung </div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
16a0ba19b2cecf27e48c0c197ae1f3a96f447949sf<div class="section">
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<h2><a name="canonicalhost" id="canonicalhost">Canonical Hostnames</a></h2>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf
af4381d113faafc97340eaaa008840c7fcbcc8fdsf <dl>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf <dt>Description:</dt>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf
af4381d113faafc97340eaaa008840c7fcbcc8fdsf <dd>The goal of this rule is to force the use of a particular
af4381d113faafc97340eaaa008840c7fcbcc8fdsf hostname, in preference to other hostnames which may be used to
cb8646cb564e6b2f7b1580b01ba4fbfd26231253sf reach the same site. For example, if you wish to force the use
cb8646cb564e6b2f7b1580b01ba4fbfd26231253sf of <strong>www.example.com</strong> instead of
cb8646cb564e6b2f7b1580b01ba4fbfd26231253sf <strong>example.com</strong>, you might use a variant of the
cb8646cb564e6b2f7b1580b01ba4fbfd26231253sf following recipe.</dd>
cb8646cb564e6b2f7b1580b01ba4fbfd26231253sf
cb8646cb564e6b2f7b1580b01ba4fbfd26231253sf <dt>Solution:</dt>
cb8646cb564e6b2f7b1580b01ba4fbfd26231253sf
af4381d113faafc97340eaaa008840c7fcbcc8fdsf <dd>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<p>For sites running on a port other than 80:</p>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<div class="example"><pre>
af4381d113faafc97340eaaa008840c7fcbcc8fdsfRewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
af4381d113faafc97340eaaa008840c7fcbcc8fdsfRewriteCond %{HTTP_HOST} !^$
efb3a31a4ca193ac8629a4c039d481c29171d6e0sfRewriteCond %{SERVER_PORT} !^80$
af4381d113faafc97340eaaa008840c7fcbcc8fdsfRewriteRule ^/?(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R]
af4381d113faafc97340eaaa008840c7fcbcc8fdsf</pre></div>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf<p>And for a site running on port 80</p>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf<div class="example"><pre>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sfRewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
efb3a31a4ca193ac8629a4c039d481c29171d6e0sfRewriteCond %{HTTP_HOST} !^$
2e0e3814627be5f1f08d890663cfa6c1f7671a4crpluemRewriteRule ^/?(.*) http://www.example.com/$1 [L,R]
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf</pre></div>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf <p>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf If you wanted to do this generically for all domain names - that
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf is, if you want to redirect <strong>example.com</strong> to
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf <strong>www.example.com</strong> for all possible values of
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf <strong>example.com</strong>, you could use the following
af4381d113faafc97340eaaa008840c7fcbcc8fdsf recipe:</p>
16a0ba19b2cecf27e48c0c197ae1f3a96f447949sf
16a0ba19b2cecf27e48c0c197ae1f3a96f447949sf<div class="example"><pre>
16a0ba19b2cecf27e48c0c197ae1f3a96f447949sfRewriteCond %{HTTP_HOST} !^www\. [NC]
16a0ba19b2cecf27e48c0c197ae1f3a96f447949sfRewriteCond %{HTTP_HOST} !^$
16a0ba19b2cecf27e48c0c197ae1f3a96f447949sfRewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R]
16a0ba19b2cecf27e48c0c197ae1f3a96f447949sf</pre></div>
16a0ba19b2cecf27e48c0c197ae1f3a96f447949sf
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf <p>These rulesets will work either in your main server configuration
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf file, or in a <code>.htaccess</code> file placed in the <code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code> of the server.</p>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf </dd>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf </dl>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf
af4381d113faafc97340eaaa008840c7fcbcc8fdsf </div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf<div class="section">
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf<h2><a name="moveddocroot" id="moveddocroot">Moved <code>DocumentRoot</code></a></h2>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf
af4381d113faafc97340eaaa008840c7fcbcc8fdsf
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf <dl>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf <dt>Description:</dt>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf <dd>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf<p>Usually the <code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code>
af4381d113faafc97340eaaa008840c7fcbcc8fdsfof the webserver directly relates to the URL "<code>/</code>".
efb3a31a4ca193ac8629a4c039d481c29171d6e0sfBut often this data is not really of top-level priority. For example,
efb3a31a4ca193ac8629a4c039d481c29171d6e0sfyou may wish for visitors, on first entering a site, to go to a
efb3a31a4ca193ac8629a4c039d481c29171d6e0sfparticular subdirectory <code>/about/</code>. This may be accomplished
af4381d113faafc97340eaaa008840c7fcbcc8fdsfusing the following ruleset:</p>
efb3a31a4ca193ac8629a4c039d481c29171d6e0sf</dd>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf
af4381d113faafc97340eaaa008840c7fcbcc8fdsf <dt>Solution:</dt>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf
af4381d113faafc97340eaaa008840c7fcbcc8fdsf <dd>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf <p>We redirect the URL <code>/</code> to
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung <code>/about/</code>:
727872d18412fc021f03969b8641810d8896820bhumbedooh </p>
0d0ba3a410038e179b695446bb149cce6264e0abnd
727872d18412fc021f03969b8641810d8896820bhumbedooh<div class="example"><pre>
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedoohRewriteEngine on
0d0ba3a410038e179b695446bb149cce6264e0abndRewriteRule <strong>^/$</strong> /about/ [<strong>R</strong>]
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh</pre></div>
727872d18412fc021f03969b8641810d8896820bhumbedooh
0d0ba3a410038e179b695446bb149cce6264e0abnd <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>
0d0ba3a410038e179b695446bb149cce6264e0abnd
0d0ba3a410038e179b695446bb149cce6264e0abnd<div class="example"><p><code>
ac082aefa89416cbdc9a1836eaf3bed9698201c8humbedoohRedirectMatch ^/$ http://example.com/about/
0d0ba3a410038e179b695446bb149cce6264e0abnd</code></p></div>
0d0ba3a410038e179b695446bb149cce6264e0abnd
0d0ba3a410038e179b695446bb149cce6264e0abnd<p>Note also that the example rewrites only the root URL. That is, it
727872d18412fc021f03969b8641810d8896820bhumbedoohrewrites a request for <code>http://example.com/</code>, but not a
0d0ba3a410038e179b695446bb149cce6264e0abndrequest for <code>http://example.com/page.html</code>. If you have in
0d0ba3a410038e179b695446bb149cce6264e0abndfact changed your document root - that is, if <strong>all</strong> of
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedoohyour content is in fact in that subdirectory, it is greatly preferable
205f749042ed530040a4f0080dbcb47ceae8a374rjungto simply change your <code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code>
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowendirective, rather than rewriting URLs.</p>
0d0ba3a410038e179b695446bb149cce6264e0abnd</dd>
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd</dl>
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd </div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
af4381d113faafc97340eaaa008840c7fcbcc8fdsf<div class="section">
<h2><a name="trailingslash" id="trailingslash">Trailing Slash Problem</a></h2>
<dl>
<dt>Description:</dt>
<dd><p>The vast majority of "trailing slash" problems can be dealt
with using the techniques discussed in the <a href="http://httpd.apache.org/docs/misc/FAQ-E.html#set-servername">FAQ
entry</a>. However, occasionally, there is a need to use mod_rewrite
to handle a case where a missing trailing slash causes a URL to
fail. This can happen, for example, after a series of complex
rewrite rules.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>The solution to this subtle problem is to let the server
add the trailing slash automatically. To do this
correctly we have to use an external redirect, so the
browser correctly requests subsequent images etc. If we
only did a internal rewrite, this would only work for the
directory page, but would go wrong when any images are
included into this page with relative URLs, because the
browser would request an in-lined object. For instance, a
request for <code>image.gif</code> in
<code>/~quux/foo/index.html</code> would become
<code>/~quux/image.gif</code> without the external
redirect!</p>
<p>So, to do this trick we write:</p>
<div class="example"><pre>
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo<strong>$</strong> foo<strong>/</strong> [<strong>R</strong>]
</pre></div>
<p>Alternately, you can put the following in a
top-level <code>.htaccess</code> file in the content directory.
But note that this creates some processing overhead.</p>
<div class="example"><pre>
RewriteEngine on
RewriteBase /~quux/
RewriteCond %{REQUEST_FILENAME} <strong>-d</strong>
RewriteRule ^(.+<strong>[^/]</strong>)$ $1<strong>/</strong> [R]
</pre></div>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="movehomedirs" id="movehomedirs">Move Homedirs to Different Webserver</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>Many webmasters have asked for a solution to the
following situation: They wanted to redirect just all
homedirs on a webserver to another webserver. They usually
need such things when establishing a newer webserver which
will replace the old one over time.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>The solution is trivial with <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>.
On the old webserver we just redirect all
<code>/~user/anypath</code> URLs to
<code>http://newserver/~user/anypath</code>.</p>
<div class="example"><pre>
RewriteEngine on
RewriteRule ^/~(.+) http://<strong>newserver</strong>/~$1 [R,L]
</pre></div>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="multipledirs" id="multipledirs">Search for pages in more than one directory</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>Sometimes it is necessary to let the webserver search
for pages in more than one directory. Here MultiViews or
other techniques cannot help.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We program a explicit ruleset which searches for the
files in the directories.</p>
<div class="example"><pre>
RewriteEngine on
# first try to find it in dir1/...
# ...and if found stop and be happy:
RewriteCond %{DOCUMENT_ROOT}/<strong>dir1</strong>/%{REQUEST_FILENAME} -f
RewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir1</strong>/$1 [L]
# second try to find it in dir2/...
# ...and if found stop and be happy:
RewriteCond %{DOCUMENT_ROOT}/<strong>dir2</strong>/%{REQUEST_FILENAME} -f
RewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir2</strong>/$1 [L]
# else go on for other Alias or ScriptAlias directives,
# etc.
RewriteRule ^(.+) - [PT]
</pre></div>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="setenvvars" id="setenvvars">Set Environment Variables According To URL Parts</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>Perhaps you want to keep status information between
requests and use the URL to encode it. But you don't want
to use a CGI wrapper for all pages just to strip out this
information.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We use a rewrite rule to strip out the status information
and remember it via an environment variable which can be
later dereferenced from within XSSI or CGI. This way a
URL <code>/foo/S=java/bar/</code> gets translated to
<code>/foo/bar/</code> and the environment variable named
<code>STATUS</code> is set to the value "java".</p>
<div class="example"><pre>
RewriteEngine on
RewriteRule ^(.*)/<strong>S=([^/]+)</strong>/(.*) $1/$3 [E=<strong>STATUS:$2</strong>]
</pre></div>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="uservhosts" id="uservhosts">Virtual Hosts Per User</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>Assume that you want to provide
<code>www.<strong>username</strong>.host.domain.com</code>
for the homepage of username via just DNS A records to the
same machine and without any virtualhosts on this
machine.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>For HTTP/1.0 requests there is no solution, but for
HTTP/1.1 requests which contain a Host: HTTP header we
can use the following ruleset to rewrite
<code>http://www.username.host.com/anypath</code>
internally to <code>/home/username/anypath</code>:</p>
<div class="example"><pre>
RewriteEngine on
RewriteCond %{<strong>HTTP_HOST</strong>} ^www\.<strong>([^.]+)</strong>\.host\.com$
RewriteRule ^(.*) /home/<strong>%1</strong>$1
</pre></div>
<p>Parentheses used in a <code class="directive"><a href="/mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> are captured into the
backreferences <code>%1</code>, <code>%2</code>, etc, while parentheses
used in <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> are
captured into the backreferences <code>$1</code>, <code>$2</code>,
etc.</p>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="redirecthome" id="redirecthome">Redirect Homedirs For Foreigners</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>We want to redirect homedir URLs to another webserver
<code>www.somewhere.com</code> when the requesting user
does not stay in the local domain
<code>ourdomain.com</code>. This is sometimes used in
virtual host contexts.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>Just a rewrite condition:</p>
<div class="example"><pre>
RewriteEngine on
RewriteCond %{REMOTE_HOST} <strong>!^.+\.ourdomain\.com$</strong>
RewriteRule ^(/~.+) http://www.somewhere.com/$1 [R,L]
</pre></div>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="redirectanchors" id="redirectanchors">Redirecting Anchors</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>By default, redirecting to an HTML anchor doesn't work,
because mod_rewrite escapes the <code>#</code> character,
turning it into <code>%23</code>. This, in turn, breaks the
redirection.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>Use the <code>[NE]</code> flag on the
<code>RewriteRule</code>. NE stands for No Escape.
</p>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="time-dependent" id="time-dependent">Time-Dependent Rewriting</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>When tricks like time-dependent content should happen a
lot of webmasters still use CGI scripts which do for
instance redirects to specialized pages. How can it be done
via <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>?</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>There are a lot of variables named <code>TIME_xxx</code>
for rewrite conditions. In conjunction with the special
lexicographic comparison patterns <code>&lt;STRING</code>,
<code>&gt;STRING</code> and <code>=STRING</code> we can
do time-dependent redirects:</p>
<div class="example"><pre>
RewriteEngine on
RewriteCond %{TIME_HOUR}%{TIME_MIN} &gt;0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} &lt;1900
RewriteRule ^foo\.html$ foo.day.html
RewriteRule ^foo\.html$ foo.night.html
</pre></div>
<p>This provides the content of <code>foo.day.html</code>
under the URL <code>foo.html</code> from
<code>07:00-19:00</code> and at the remaining time the
contents of <code>foo.night.html</code>. Just a nice
feature for a homepage...</p>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="backward-compatibility" id="backward-compatibility">Backward Compatibility for YYYY to XXXX migration</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>How can we make URLs backward compatible (still
existing virtually) after migrating <code>document.YYYY</code>
to <code>document.XXXX</code>, e.g. after translating a
bunch of <code>.html</code> files to <code>.phtml</code>?</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We just rewrite the name to its basename and test for
existence of the new extension. If it exists, we take
that name, else we rewrite the URL to its original state.</p>
<div class="example"><pre>
# backward compatibility ruleset for
# rewriting document.html to document.phtml
# when and only when document.phtml exists
# but no longer document.html
RewriteEngine on
RewriteBase /~quux/
# parse out basename, but remember the fact
RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes]
# rewrite to document.phtml if exists
RewriteCond %{REQUEST_FILENAME}.phtml -f
RewriteRule ^(.*)$ $1.phtml [S=1]
# else reverse the previous basename cutout
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $1.html
</pre></div>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="old-to-new" id="old-to-new">From Old to New (intern)</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>Assume we have recently renamed the page
<code>foo.html</code> to <code>bar.html</code> and now want
to provide the old URL for backward compatibility. Actually
we want that users of the old URL even not recognize that
the pages was renamed.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We rewrite the old URL to the new one internally via the
following rule:</p>
<div class="example"><pre>
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^<strong>foo</strong>\.html$ <strong>bar</strong>.html
</pre></div>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="old-to-new-extern" id="old-to-new-extern">From Old to New (extern)</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>Assume again that we have recently renamed the page
<code>foo.html</code> to <code>bar.html</code> and now want
to provide the old URL for backward compatibility. But this
time we want that the users of the old URL get hinted to
the new one, i.e. their browsers Location field should
change, too.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We force a HTTP redirect to the new URL which leads to a
change of the browsers and thus the users view:</p>
<div class="example"><pre>
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^<strong>foo</strong>\.html$ <strong>bar</strong>.html [<strong>R</strong>]
</pre></div>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="static-to-dynamic" id="static-to-dynamic">From Static to Dynamic</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>How can we transform a static page
<code>foo.html</code> into a dynamic variant
<code>foo.cgi</code> in a seamless way, i.e. without notice
by the browser/user.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We just rewrite the URL to the CGI-script and force the
handler to be <strong>cgi-script</strong> so that it is
executed as a CGI program.
This way a request to <code>/~quux/foo.html</code>
internally leads to the invocation of
<code>/~quux/foo.cgi</code>.</p>
<div class="example"><pre>
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo\.<strong>html</strong>$ foo.<strong>cgi</strong> [H=<strong>cgi-script</strong>]
</pre></div>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="blocking-of-robots" id="blocking-of-robots">Blocking of Robots</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>How can we block a really annoying robot from
retrieving pages of a specific webarea? A
<code>/robots.txt</code> file containing entries of the
"Robot Exclusion Protocol" is typically not enough to get
rid of such a robot.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We use a ruleset which forbids the URLs of the webarea
<code>/~quux/foo/arc/</code> (perhaps a very deep
directory indexed area where the robot traversal would
create big server load). We have to make sure that we
forbid access only to the particular robot, i.e. just
forbidding the host where the robot runs is not enough.
This would block users from this host, too. We accomplish
this by also matching the User-Agent HTTP header
information.</p>
<div class="example"><pre>
RewriteCond %{HTTP_USER_AGENT} ^<strong>NameOfBadRobot</strong>.*
RewriteCond %{REMOTE_ADDR} ^<strong>123\.45\.67\.[8-9]</strong>$
RewriteRule ^<strong>/~quux/foo/arc/</strong>.+ - [<strong>F</strong>]
</pre></div>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="blocked-inline-images" id="blocked-inline-images">Forbidding Image "Hotlinking"</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>The following technique forbids the practice of other sites
including your images inline in their pages. This practice is
often referred to as "hotlinking", and results in
your bandwidth being used to serve content for someone else's
site.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>This technique relies on the value of the
<code>HTTP_REFERER</code> variable, which is optional. As
such, it's possible for some people to circumvent this
limitation. However, most users will experience the failed
request, which should, over time, result in the image being
removed from that other site.</p>
<p>There are several ways that you can handle this
situation.</p>
<p>In this first example, we simply deny the request, if it didn't
initiate from a page on our site. For the purpose of this example,
we assume that our site is <code>www.example.com</code>.</p>
<div class="example"><pre>
RewriteCond %{HTTP_REFERER} <strong>!^$</strong>
RewriteCond %{HTTP_REFERER} !www.example.com [NC]
RewriteRule <strong>\.(gif|jpg|png)$</strong> - [F,NC]
</pre></div>
<p>In this second example, instead of failing the request, we display
an alternate image instead.</p>
<div class="example"><pre>
RewriteCond %{HTTP_REFERER} <strong>!^$</strong>
RewriteCond %{HTTP_REFERER} !www.example.com [NC]
RewriteRule <strong>\.(gif|jpg|png)$</strong> /images/go-away.png [R,NC]
</pre></div>
<p>In the third example, we redirect the request to an image on some
third-party site.</p>
<div class="example"><pre>
RewriteCond %{HTTP_REFERER} <strong>!^$</strong>
RewriteCond %{HTTP_REFERER} !www.example.com [NC]
RewriteRule <strong>\.(gif|jpg|png)$</strong> http://other.site.com/image.gif [R,NC]
</pre></div>
<p>Of these techniques, the last two tend to be the most effective
in getting people to stop hotlinking your images, because they will
simply not see the image that they expected to see.</p>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="proxy-deny" id="proxy-deny">Proxy Deny</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>How can we forbid a certain host or even a user of a
special host from using the Apache proxy?</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We first have to make sure <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
is below(!) <code class="module"><a href="/mod/mod_proxy.html">mod_proxy</a></code> in the Configuration
file when compiling the Apache webserver. This way it gets
called <em>before</em> <code class="module"><a href="/mod/mod_proxy.html">mod_proxy</a></code>. Then we
configure the following for a host-dependent deny...</p>
<div class="example"><pre>
RewriteCond %{REMOTE_HOST} <strong>^badhost\.mydomain\.com$</strong>
RewriteRule !^http://[^/.]\.mydomain.com.* - [F]
</pre></div>
<p>...and this one for a user@host-dependent deny:</p>
<div class="example"><pre>
RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} <strong>^badguy@badhost\.mydomain\.com$</strong>
RewriteRule !^http://[^/.]\.mydomain.com.* - [F]
</pre></div>
</dd>
</dl>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="external-rewriting" id="external-rewriting">External Rewriting Engine</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>A FAQ: How can we solve the FOO/BAR/QUUX/etc.
problem? There seems no solution by the use of
<code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>...</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>Use an external <code class="directive"><a href="/mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>, i.e. a program which acts
like a <code class="directive"><a href="/mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>. It is run once on startup of Apache
receives the requested URLs on <code>STDIN</code> and has
to put the resulting (usually rewritten) URL on
<code>STDOUT</code> (same order!).</p>
<div class="example"><pre>
RewriteEngine on
RewriteMap quux-map <strong>prg:</strong>/path/to/map.quux.pl
RewriteRule ^/~quux/(.*)$ /~quux/<strong>${quux-map:$1}</strong>
</pre></div>
<div class="example"><pre>
#!/path/to/perl
# disable buffered I/O which would lead
# to deadloops for the Apache server
$| = 1;
# read URLs one per line from stdin and
# generate substitution URL on stdout
while (&lt;&gt;) {
s|^foo/|bar/|;
print $_;
}
</pre></div>
<p>This is a demonstration-only example and just rewrites
all URLs <code>/~quux/foo/...</code> to
<code>/~quux/bar/...</code>. Actually you can program
whatever you like. But notice that while such maps can be
<strong>used</strong> also by an average user, only the
system administrator can <strong>define</strong> it.</p>
</dd>
</dl>
</div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="/en/rewrite/rewrite_guide.html" title="English">&nbsp;en&nbsp;</a></p>
</div><div id="footer">
<p class="apache">Copyright 2008 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>
<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>
</body></html>