remapping.xml revision 9d7ecbe91ba405b87f677ec1e322b8e92533172c
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<?xml version="1.0" encoding="UTF-8" ?>
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<!DOCTYPE manualpage SYSTEM "/style/manualpage.dtd">
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<?xml-stylesheet type="text/xsl" href="/style/manual.en.xsl"?>
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<!-- $LastChangedRevision$ -->
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<!--
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen Licensed to the Apache Software Foundation (ASF) under one or more
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen contributor license agreements. See the NOTICE file distributed with
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen this work for additional information regarding copyright ownership.
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen The ASF licenses this file to You under the Apache License, Version 2.0
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen (the "License"); you may not use this file except in compliance with
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen the License. You may obtain a copy of the License at
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen http://www.apache.org/licenses/LICENSE-2.0
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen Unless required by applicable law or agreed to in writing, software
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen distributed under the License is distributed on an "AS IS" BASIS,
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen See the License for the specific language governing permissions and
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen limitations under the License.
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen-->
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<manualpage metafile="remapping.xml.meta">
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen <parentdocument href="./">Rewrite</parentdocument>
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<title>Redirecting and Remapping with mod_rewrite</title>
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<summary>
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<p>This document supplements the <module>mod_rewrite</module>
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<a href="/mod/mod_rewrite.html">reference documentation</a>. It describes
4a1711ab536d965e63d7be80e29f912287fe70b2rbowenhow you can use <module>mod_rewrite</module> to redirect and remap
4a1711ab536d965e63d7be80e29f912287fe70b2rbowenrequest. This includes many examples of common uses of mod_rewrite,
4a1711ab536d965e63d7be80e29f912287fe70b2rbowenincluding detailed descriptions of how each works.</p>
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<note type="warning">Note that many of these examples won't work unchanged in your
4a1711ab536d965e63d7be80e29f912287fe70b2rbowenparticular server configuration, so it's important that you understand
4a1711ab536d965e63d7be80e29f912287fe70b2rbowenthem, rather than merely cutting and pasting the examples into your
1a1f0f42c4b3840641897888fec7792af8ba384frbowenconfiguration.</note>
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen</summary>
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<seealso><a href="/mod/mod_rewrite.html">Module documentation</a></seealso>
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<seealso><a href="intro.html">mod_rewrite introduction</a></seealso>
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen<!--<seealso><a href="remapping.html">Redirection and remapping</a></seealso>-->
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<seealso><a href="access.html">Controlling access</a></seealso>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<seealso><a href="vhosts.html">Virtual hosts</a></seealso>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<seealso><a href="proxy.html">Proxying</a></seealso>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<seealso><a href="rewritemap.html">Using RewriteMap</a></seealso>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<seealso><a href="advanced.html">Advanced techniques and tricks</a></seealso>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<seealso><a href="avoid.html">When not to use mod_rewrite</a></seealso>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<section id="old-to-new">
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <title>From Old to New (internal)</title>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dl>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dt>Description:</dt>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <p>Assume we have recently renamed the page
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <code>foo.html</code> to <code>bar.html</code> and now want
1a1f0f42c4b3840641897888fec7792af8ba384frbowen to provide the old URL for backward compatibility. However,
1a1f0f42c4b3840641897888fec7792af8ba384frbowen we want that users of the old URL even not recognize that
1a1f0f42c4b3840641897888fec7792af8ba384frbowen the pages was renamed - that is, we don't want the address to
1a1f0f42c4b3840641897888fec7792af8ba384frbowen change in their browser.</p>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen </dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dt>Solution:</dt>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <p>We rewrite the old URL to the new one internally via the
1a1f0f42c4b3840641897888fec7792af8ba384frbowen following rule:</p>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<example>
1a1f0f42c4b3840641897888fec7792af8ba384frbowenRewriteEngine on<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowenRewriteRule ^<strong>/old</strong>\.html$ <strong>/new</strong>.html [PT]
1a1f0f42c4b3840641897888fec7792af8ba384frbowen</example>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen </dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen </dl>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen</section>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<section id="old-to-new-extern">
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <title>Rewriting From Old to New (external)</title>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dl>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dt>Description:</dt>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <p>Assume again that we have recently renamed the page
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <code>foo.html</code> to <code>bar.html</code> and now want
1a1f0f42c4b3840641897888fec7792af8ba384frbowen to provide the old URL for backward compatibility. But this
1a1f0f42c4b3840641897888fec7792af8ba384frbowen time we want that the users of the old URL get hinted to
1a1f0f42c4b3840641897888fec7792af8ba384frbowen the new one, i.e. their browsers Location field should
1a1f0f42c4b3840641897888fec7792af8ba384frbowen change, too.</p>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen </dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dt>Solution:</dt>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <p>We force a HTTP redirect to the new URL which leads to a
1a1f0f42c4b3840641897888fec7792af8ba384frbowen change of the browsers and thus the users view:</p>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<example>
1a1f0f42c4b3840641897888fec7792af8ba384frbowenRewriteEngine on<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowenRewriteRule ^<strong>/foo</strong>\.html$ <strong>bar</strong>.html [<strong>R</strong>]
1a1f0f42c4b3840641897888fec7792af8ba384frbowen</example>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen</dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<dt>Discussion</dt>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <p>In this example, as contrasted to the <a
1a1f0f42c4b3840641897888fec7792af8ba384frbowen href="#old-to-new-intern">internal</a> example above, we can simply
1a1f0f42c4b3840641897888fec7792af8ba384frbowen use the Redirect directive. mod_rewrite was used in that earlier
1a1f0f42c4b3840641897888fec7792af8ba384frbowen example in order to hide the redirect from the client:</p>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <example>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen Redirect /foo.html /bar.html
67398034ea8b919da6274d4e178661559e1eb1e2rbowen </example>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen
67398034ea8b919da6274d4e178661559e1eb1e2rbowen </dd>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen </dl>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen
67398034ea8b919da6274d4e178661559e1eb1e2rbowen</section>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen
67398034ea8b919da6274d4e178661559e1eb1e2rbowen<section id="movehomedirs">
67398034ea8b919da6274d4e178661559e1eb1e2rbowen
67398034ea8b919da6274d4e178661559e1eb1e2rbowen <title>Resource Moved to Another Server</title>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen
67398034ea8b919da6274d4e178661559e1eb1e2rbowen <dl>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen <dt>Description:</dt>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen
67398034ea8b919da6274d4e178661559e1eb1e2rbowen <dd>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen <p>If a resource has moved to another server, you may wish to have
67398034ea8b919da6274d4e178661559e1eb1e2rbowen URLs continue to work for a time on the old server while people
67398034ea8b919da6274d4e178661559e1eb1e2rbowen update their bookmarks.</p>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen </dd>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen
67398034ea8b919da6274d4e178661559e1eb1e2rbowen <dt>Solution:</dt>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen
67398034ea8b919da6274d4e178661559e1eb1e2rbowen <dd>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen <p>You can use <module>mod_rewrite</module> to redirect these URLs
67398034ea8b919da6274d4e178661559e1eb1e2rbowen to the new server, but you might also consider using the Redirect
67398034ea8b919da6274d4e178661559e1eb1e2rbowen or RedirectMatch directive.</p>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen
67398034ea8b919da6274d4e178661559e1eb1e2rbowen<example><title>With mod_rewrite</title>
67398034ea8b919da6274d4e178661559e1eb1e2rbowenRewriteEngine on<br />
67398034ea8b919da6274d4e178661559e1eb1e2rbowenRewriteRule ^/docs/(.+) http://new.example.com/docs/$1 [R,L]
67398034ea8b919da6274d4e178661559e1eb1e2rbowen</example>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen
67398034ea8b919da6274d4e178661559e1eb1e2rbowen<example><title>With RedirectMatch</title>
67398034ea8b919da6274d4e178661559e1eb1e2rbowenRedirectMatch ^/docs/(.*) http://new.example.com/docs/$1
67398034ea8b919da6274d4e178661559e1eb1e2rbowen</example>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen
67398034ea8b919da6274d4e178661559e1eb1e2rbowen<example><title>With Redirect</title>
67398034ea8b919da6274d4e178661559e1eb1e2rbowenRedirect /docs/ http://new.example.com/docs/
67398034ea8b919da6274d4e178661559e1eb1e2rbowen</example>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen </dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen </dl>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen</section>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<section id="static-to-dynamic">
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <title>From Static to Dynamic</title>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dl>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dt>Description:</dt>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <p>How can we transform a static page
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <code>foo.html</code> into a dynamic variant
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <code>foo.cgi</code> in a seamless way, i.e. without notice
1a1f0f42c4b3840641897888fec7792af8ba384frbowen by the browser/user.</p>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen </dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dt>Solution:</dt>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <p>We just rewrite the URL to the CGI-script and force the
1a1f0f42c4b3840641897888fec7792af8ba384frbowen handler to be <strong>cgi-script</strong> so that it is
1a1f0f42c4b3840641897888fec7792af8ba384frbowen executed as a CGI program.
1a1f0f42c4b3840641897888fec7792af8ba384frbowen This way a request to <code>/~quux/foo.html</code>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen internally leads to the invocation of
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <code>/~quux/foo.cgi</code>.</p>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<example>
1a1f0f42c4b3840641897888fec7792af8ba384frbowenRewriteEngine on<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowenRewriteBase /~quux/<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowenRewriteRule ^foo\.<strong>html</strong>$ foo.<strong>cgi</strong> [H=<strong>cgi-script</strong>]
1a1f0f42c4b3840641897888fec7792af8ba384frbowen</example>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen </dd>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen </dl>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen</section>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<section id="backward-compatibility">
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <title>Backward Compatibility for file extension change</title>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <dl>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <dt>Description:</dt>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <dd>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <p>How can we make URLs backward compatible (still
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen existing virtually) after migrating <code>document.YYYY</code>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen to <code>document.XXXX</code>, e.g. after translating a
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen bunch of <code>.html</code> files to <code>.php</code>?</p>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen </dd>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <dt>Solution:</dt>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <dd>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <p>We rewrite the name to its basename and test for
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen existence of the new extension. If it exists, we take
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen that name, else we rewrite the URL to its original state.</p>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<example>
67398034ea8b919da6274d4e178661559e1eb1e2rbowen# backward compatibility ruleset for<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen# rewriting document.html to document.php<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen# when and only when document.php exists<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen&lt;Directory /var/www/htdocs&gt;<br />
67398034ea8b919da6274d4e178661559e1eb1e2rbowen<indent>
67398034ea8b919da6274d4e178661559e1eb1e2rbowenRewriteEngine on<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRewriteBase /var/www/htdocs<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRewriteCond $1.php -f<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRewriteCond $1.html !-f<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRewriteRule ^(.*).html$ $1.php<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen</indent>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen&lt;/Directory&gt;
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen</example>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen </dd>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <dt>Discussion</dt>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <dd>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <p>This example uses an often-overlooked feature of mod_rewrite,
67398034ea8b919da6274d4e178661559e1eb1e2rbowen by taking advantage of the order of execution of the ruleset. In
73036acae345bfcd48167c09e7ee2f0f0a19df8arbowen particular, mod_rewrite evaluates the left-hand-side of the
73036acae345bfcd48167c09e7ee2f0f0a19df8arbowen RewriteRule before it evaluates the RewriteCond directives.
73036acae345bfcd48167c09e7ee2f0f0a19df8arbowen Consequently, $1 is already defined by the time the RewriteCond
73036acae345bfcd48167c09e7ee2f0f0a19df8arbowen directives are evaluated. This allows us to test for the existence
73036acae345bfcd48167c09e7ee2f0f0a19df8arbowen of the original (<code>document.html</code>) and target
73036acae345bfcd48167c09e7ee2f0f0a19df8arbowen (<code>document.php</code>) files using the same base filename.</p>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <p>This ruleset is designed to use in a per-directory context (In a
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen &lt;Directory&gt; block or in a .htaccess file), so that the
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <code>-f</code> checks are looking at the correct directory path.
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen You may need to set a <directive
1a1f0f42c4b3840641897888fec7792af8ba384frbowen module="mod_rewite">RewriteBase</directive> directive to specify the
1a1f0f42c4b3840641897888fec7792af8ba384frbowen directory base that you're working in.</p>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen </dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen </dl>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen</section>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<section id="canonicalhost">
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<title>Canonical Hostnames</title>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dl>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dt>Description:</dt>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dd>The goal of this rule is to force the use of a particular
1a1f0f42c4b3840641897888fec7792af8ba384frbowen hostname, in preference to other hostnames which may be used to
1a1f0f42c4b3840641897888fec7792af8ba384frbowen reach the same site. For example, if you wish to force the use
1a1f0f42c4b3840641897888fec7792af8ba384frbowen of <strong>www.example.com</strong> instead of
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <strong>example.com</strong>, you might use a variant of the
1a1f0f42c4b3840641897888fec7792af8ba384frbowen following recipe.</dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dt>Solution:</dt>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen <dd>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<p>The very best way to solve this doesn't involve mod_rewrite at all,
1a1f0f42c4b3840641897888fec7792af8ba384frbowenbut rather uses the <directive module="alias">Redirect</directive>
1a1f0f42c4b3840641897888fec7792af8ba384frbowendirective placed in a virtual host for the non-canonical
1a1f0f42c4b3840641897888fec7792af8ba384frbowenhostname(s).</p>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<example>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen&lt;VirtualHost *:80&gt;<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<indent>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen ServerName undesired.example.com<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowen ServerAlias example.com notthis.example.com<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowen Redirect / http://www.example.com/<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowen</indent>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen&lt;/VirtualHost&gt;<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowen&lt;VirtualHost *:80&gt;<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<indent>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen ServerName www.example.com<br />
1a1f0f42c4b3840641897888fec7792af8ba384frbowen</indent>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen&lt;/VirtualHost&gt;
1a1f0f42c4b3840641897888fec7792af8ba384frbowen</example>
1a1f0f42c4b3840641897888fec7792af8ba384frbowen
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<p>You can alternatively accomplish this using the
1a1f0f42c4b3840641897888fec7792af8ba384frbowen<directive module="core" type="section">If</directive>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowendirective:</p>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<example>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen&lt;If "%{HTTP_HOST} != 'www.example.com'"&gt;<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<indent>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRedirect / http://www.example.com/
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen</indent>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen&lt;/If&gt;
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen</example>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<p>Or, for example, to redirect a portion of your site to HTTPS, you
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenmight do the following:</p>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<example>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen&lt;If "%{SERVER_PROTOCOL} != 'HTTPS'"&gt;<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<indent>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRedirect /admin/ https://www.example.com/admin/
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen</indent>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen&lt;/If&gt;
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen</example>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<p>If, for whatever reason, you still want to use <code>mod_rewrite</code>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen- if, for example, you need this to work with a larger set of RewriteRules -
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenyou might use one of the recipes below.</p>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<p>For sites running on a port other than 80:</p>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<example>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRewriteCond %{HTTP_HOST} !^www\.example\.com [NC]<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRewriteCond %{HTTP_HOST} !^$<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRewriteCond %{SERVER_PORT} !^80$<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRewriteRule ^/?(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen</example>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<p>And for a site running on port 80</p>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen<example>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRewriteCond %{HTTP_HOST} !^www\.example\.com [NC]<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRewriteCond %{HTTP_HOST} !^$<br />
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowenRewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen</example>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <p>
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen If you wanted to do this generically for all domain names - that
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen is, if you want to redirect <strong>example.com</strong> to
13e125c14cbafff43783b7f3aed11de6d4cb5b14rbowen <strong>www.example.com</strong> for all possible values of
4a1711ab536d965e63d7be80e29f912287fe70b2rbowen <strong>example.com</strong>, you could use the following
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen recipe:</p>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen<example>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowenRewriteCond %{HTTP_HOST} !^www\. [NC]<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowenRewriteCond %{HTTP_HOST} !^$<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowenRewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen</example>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen <p>These rulesets will work either in your main server configuration
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen file, or in a <code>.htaccess</code> file placed in the <directive
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen module="core">DocumentRoot</directive> of the server.</p>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen </dd>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen </dl>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen</section>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen<section id="multipledirs">
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen <title>Search for pages in more than one directory</title>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen <dl>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen <dt>Description:</dt>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen <dd>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen <p>A particular resource might exist in one of several places, and
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen we want to look in those places for the resource when it is
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen requested. Perhaps we've recently rearranged our directory
4634fe331f9689ad14057c5ca77d285b50ce08c4rbowen structure, dividing content into several locations.</p>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen </dd>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen <dt>Solution:</dt>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen <dd>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen <p>The following ruleset searches in two directories to find the
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen resource, and, if not finding it in either place, will attempt to
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen just serve it out of the location requested.</p>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen<example>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowenRewriteEngine on<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen# first try to find it in dir1/...<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen# ...and if found stop and be happy:<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowenRewriteCond %{DOCUMENT_ROOT}/<strong>dir1</strong>/%{REQUEST_URI} -f<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowenRewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir1</strong>/$1 [L]<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen# second try to find it in dir2/...<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen# ...and if found stop and be happy:<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowenRewriteCond %{DOCUMENT_ROOT}/<strong>dir2</strong>/%{REQUEST_URI} -f<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowenRewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir2</strong>/$1 [L]<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen# else go on for other Alias or ScriptAlias directives,<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen# etc.<br />
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowenRewriteRule ^ - [PT]
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen</example>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen </dd>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen </dl>
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen</section>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen<section id="archive-access-multiplexer">
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen <title>Redirecting to Geographically Distributed Servers</title>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen <dl>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen <dt>Description:</dt>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen <dd>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen <p>We have numerous mirrors of our website, and want to redirect
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen people to the one that is located in the country where they are
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen located.</p>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen </dd>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen <dt>Solution:</dt>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen <dd>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen <p>Looking at the hostname of the requesting client, we determine
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen which country they are coming from. If we can't do a lookup on their
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen IP address, we fall back to a default server.</p>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen <p>We'll use a <directive module="mod_rewrite">RewriteMap</directive>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen directive to build a list of servers that we wish to use.</p>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen<example>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowenHostnameLookups on<br />
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowenRewriteEngine on<br />
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowenRewriteMap multiplex txt:/path/to/map.mirrors<br />
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowenRewriteCond %{REMOTE_HOST} ([a-z]+)$ [NC]<br />
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowenRewriteRule ^/(.*)$ ${multiplex:<strong>%1</strong>|http://www.example.com/}$1 [R,L]
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen</example>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen<example>
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen## map.mirrors -- Multiplexing Map<br />
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen<br />
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowende http://www.example.de/<br />
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowenuk http://www.example.uk/<br />
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowencom http://www.example.com/<br />
32e1018ca3ab0e0bd089fd38ce8d97bb7a607639rbowen##EOF##
aa3ddf73a3cb180f88a18742f363f2cb9a0c5cderbowen</example>
856d8f134df3b2d23a76717121e042877f85ae3brbowen </dd>
856d8f134df3b2d23a76717121e042877f85ae3brbowen
856d8f134df3b2d23a76717121e042877f85ae3brbowen <dt>Discussion</dt>
856d8f134df3b2d23a76717121e042877f85ae3brbowen <dd>
856d8f134df3b2d23a76717121e042877f85ae3brbowen <note type="warning">This ruleset relies on
856d8f134df3b2d23a76717121e042877f85ae3brbowen <directive module="core">HostNameLookups</directive>
856d8f134df3b2d23a76717121e042877f85ae3brbowen being set <code>on</code>, which can be
856d8f134df3b2d23a76717121e042877f85ae3brbowen a significant performance hit.</note>
856d8f134df3b2d23a76717121e042877f85ae3brbowen
856d8f134df3b2d23a76717121e042877f85ae3brbowen <p>The <directive module="mod_rewrite">RewriteCond</directive>
856d8f134df3b2d23a76717121e042877f85ae3brbowen directive captures the last portion of the hostname of the
856d8f134df3b2d23a76717121e042877f85ae3brbowen requesting client - the country code - and the following RewriteRule
856d8f134df3b2d23a76717121e042877f85ae3brbowen uses that value to look up the appropriate mirror host in the map
856d8f134df3b2d23a76717121e042877f85ae3brbowen file.</p>
856d8f134df3b2d23a76717121e042877f85ae3brbowen </dd>
856d8f134df3b2d23a76717121e042877f85ae3brbowen </dl>
856d8f134df3b2d23a76717121e042877f85ae3brbowen
856d8f134df3b2d23a76717121e042877f85ae3brbowen</section>
856d8f134df3b2d23a76717121e042877f85ae3brbowen
856d8f134df3b2d23a76717121e042877f85ae3brbowen<section id="browser-dependent-content">
856d8f134df3b2d23a76717121e042877f85ae3brbowen
856d8f134df3b2d23a76717121e042877f85ae3brbowen <title>Browser Dependent Content</title>
856d8f134df3b2d23a76717121e042877f85ae3brbowen
856d8f134df3b2d23a76717121e042877f85ae3brbowen <dl>
856d8f134df3b2d23a76717121e042877f85ae3brbowen <dt>Description:</dt>
856d8f134df3b2d23a76717121e042877f85ae3brbowen
856d8f134df3b2d23a76717121e042877f85ae3brbowen <dd>
856d8f134df3b2d23a76717121e042877f85ae3brbowen <p>We wish to provide different content based on the browser, or
856d8f134df3b2d23a76717121e042877f85ae3brbowen user-agent, which is requesting the content.</p>
856d8f134df3b2d23a76717121e042877f85ae3brbowen </dd>
856d8f134df3b2d23a76717121e042877f85ae3brbowen
856d8f134df3b2d23a76717121e042877f85ae3brbowen <dt>Solution:</dt>
856d8f134df3b2d23a76717121e042877f85ae3brbowen
856d8f134df3b2d23a76717121e042877f85ae3brbowen <dd>
856d8f134df3b2d23a76717121e042877f85ae3brbowen <p>We have to decide, based on the HTTP header "User-Agent",
856d8f134df3b2d23a76717121e042877f85ae3brbowen which content to serve. The following config
856d8f134df3b2d23a76717121e042877f85ae3brbowen does the following: If the HTTP header "User-Agent"
856d8f134df3b2d23a76717121e042877f85ae3brbowen contains "Mozilla/3", the page <code>foo.html</code>
856d8f134df3b2d23a76717121e042877f85ae3brbowen is rewritten to <code>foo.NS.html</code> and the
856d8f134df3b2d23a76717121e042877f85ae3brbowen rewriting stops. If the browser is "Lynx" or "Mozilla" of
856d8f134df3b2d23a76717121e042877f85ae3brbowen version 1 or 2, the URL becomes <code>foo.20.html</code>.
856d8f134df3b2d23a76717121e042877f85ae3brbowen All other browsers receive page <code>foo.32.html</code>.
856d8f134df3b2d23a76717121e042877f85ae3brbowen This is done with the following ruleset:</p>
856d8f134df3b2d23a76717121e042877f85ae3brbowen
856d8f134df3b2d23a76717121e042877f85ae3brbowen<example>
4a1711ab536d965e63d7be80e29f912287fe70b2rbowenRewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/3</strong>.*<br />
RewriteRule ^foo\.html$ foo.<strong>NS</strong>.html [<strong>L</strong>]<br />
<br />
RewriteCond %{HTTP_USER_AGENT} ^<strong>Lynx/</strong> [OR]<br />
RewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/[12]</strong><br />
RewriteRule ^foo\.html$ foo.<strong>20</strong>.html [<strong>L</strong>]<br />
<br />
RewriteRule ^foo\.html$ foo.<strong>32</strong>.html [<strong>L</strong>]
</example>
</dd>
</dl>
</section>
<section id="canonicalurl">
<title>Canonical URLs</title>
<dl>
<dt>Description:</dt>
<dd>
<p>On some webservers there is more than one URL for a
resource. Usually there are canonical URLs (which are be
actually used and distributed) and those which are just
shortcuts, internal ones, and so on. Independent of which URL the
user supplied with the request, they should finally see the
canonical one in their browser address bar.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We do an external HTTP redirect for all non-canonical
URLs to fix them in the location view of the Browser and
for all subsequent requests. In the example ruleset below
we replace <code>/puppies</code> and <code>/canines</code>
by the canonical <code>/dogs</code>.</p>
<example>
RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R]
</example>
</dd>
<dt>Discussion:</dt>
<dd>
This should really be accomplished with Redirect or RedirectMatch
directives:
<example>
RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2
</example>
</dd>
</dl>
</section>
<section id="moveddocroot">
<title>Moved <code>DocumentRoot</code></title>
<dl>
<dt>Description:</dt>
<dd>
<p>Usually the <directive module="core">DocumentRoot</directive>
of the webserver directly relates to the URL "<code>/</code>".
But often this data is not really of top-level priority. For example,
you may wish for visitors, on first entering a site, to go to a
particular subdirectory <code>/about/</code>. This may be accomplished
using the following ruleset:</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We redirect the URL <code>/</code> to
<code>/about/</code>:
</p>
<example>
RewriteEngine on<br />
RewriteRule <strong>^/$</strong> /about/ [<strong>R</strong>]
</example>
<p>Note that this can also be handled using the <directive
module="mod_alias">RedirectMatch</directive> directive:</p>
<example>
RedirectMatch ^/$ http://example.com/about/
</example>
<p>Note also that the example rewrites only the root URL. That is, it
rewrites a request for <code>http://example.com/</code>, but not a
request for <code>http://example.com/page.html</code>. If you have in
fact changed your document root - that is, if <strong>all</strong> of
your content is in fact in that subdirectory, it is greatly preferable
to simply change your <directive module="core">DocumentRoot</directive>
directive, or move all of the content up one directory,
rather than rewriting URLs.</p>
</dd>
</dl>
</section>
<section id="fallback-resource">
<title>Fallback Resource</title>
<dl>
<dt>Description:</dt>
<dd>You want a single resource (say, a certain file, like index.php) to
handle all requests that come to a particular directory, except those
that should go to an existing resource such as an image, or a css file.</dd>
<dt>Solution:</dt>
<dd>
<p>As of version 2.2.16, you should use the <directive
module="mod_dir">FallbackResource</directive> directive for this:</p>
<example>
&lt;Directory /var/www/my_blog&gt;<br />
<indent>
FallbackResource index.php<br />
</indent>
&lt;/Directory&gt;
</example>
<p>However, in earlier versions of Apache, or if your needs are more
complicated than this, you can use a variation of the following rewrite
set to accomplish the same thing:</p>
<example>
&lt;Directory /var/www/my_blog&gt;<br />
<indent>
RewriteBase /my_blog<br />
<br />
RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-f<br />
RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-d<br />
RewriteRule ^ index.php [PT]<br />
</indent>
&lt;/Directory&gt;
</example>
<p>If, on the other hand, you wish to pass the requested URI as a query
string argument to index.php, you can replace that RewriteRule with:</p>
<example>
RewriteRule (.*) index.php?$1 [PT,QSA]
</example>
<p>Note that these rulesets can be used in a <code>.htaccess</code>
file, as well as in a &lt;Directory&gt; block.</p>
</dd>
</dl>
</section>
</manualpage>