mod_rewrite.html revision 2eaf662cbc81e823e8d9aeb8d54e69e63032493e
20bc99aa3ad84a338b7a9c1edc14682bce25d761vboxsync<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
20bc99aa3ad84a338b7a9c1edc14682bce25d761vboxsync "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
20bc99aa3ad84a338b7a9c1edc14682bce25d761vboxsync<!--%hypertext -->
20bc99aa3ad84a338b7a9c1edc14682bce25d761vboxsync<!-- Documentation for the mod_rewrite Apache module -->
20bc99aa3ad84a338b7a9c1edc14682bce25d761vboxsync <meta name="generator" content="HTML Tidy, see www.w3.org" />
20bc99aa3ad84a338b7a9c1edc14682bce25d761vboxsync <!-- Background white, links blue (unvisited), navy (visited), red (active) -->
20bc99aa3ad84a338b7a9c1edc14682bce25d761vboxsync <body bgcolor="#FFFFFF" text="#000000" link="#0000FF"
20bc99aa3ad84a338b7a9c1edc14682bce25d761vboxsync <blockquote>
20bc99aa3ad84a338b7a9c1edc14682bce25d761vboxsync <!-- page indentation -->
20bc99aa3ad84a338b7a9c1edc14682bce25d761vboxsync <!--#include virtual="header.html" -->
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync URL Rewriting Engine</h1>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <p>This module provides a rule-based rewriting engine to
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rewrite requested URLs on the fly.</p>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rel="Help"><strong>Status:</strong></a> Extension<br />
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rewrite_module<br />
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rel="Help"><strong>Compatibility:</strong></a> Available in
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync Apache 1.2 and later.</p>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <blockquote>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <em>``The great thing about mod_rewrite is it gives you
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync all the configurability and flexibility of Sendmail.
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync The downside to mod_rewrite is that it gives you all
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync the configurability and flexibility of Sendmail.''</em>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync -- Brian Behlendorf<br />
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync Apache Group
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync </blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync </blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync </blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <em>`` Despite the tons of examples and docs,
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync mod_rewrite is voodoo. Damned cool voodoo, but still
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync voodoo. ''</em>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync -- Brian Moore<br />
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync bem@news.cmc.net
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync </blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync </blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync </blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync Welcome to mod_rewrite, the Swiss Army Knife of URL
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync manipulation!
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <p>This module uses a rule-based rewriting engine (based on a
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync regular-expression parser) to rewrite requested URLs on the
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync fly. It supports an unlimited number of rules and an
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync unlimited number of attached rule conditions for each rule to
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync provide a really flexible and powerful URL manipulation
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync mechanism. The URL manipulations can depend on various tests,
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync for instance server variables, environment variables, HTTP
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync headers, time stamps and even external database lookups in
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync various formats can be used to achieve a really granular URL
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync matching.</p>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <p>This module operates on the full URLs (including the
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync path-info part) both in per-server context
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync (<code>httpd.conf</code>) and per-directory context
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync (<code>.htaccess</code>) and can even generate query-string
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync parts on result. The rewritten result can lead to internal
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync sub-processing, external request redirection or even to an
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync internal proxy throughput.</p>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <p>But all this functionality and flexibility has its
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync drawback: complexity. So don't expect to understand this
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync entire module in just one day.</p>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <p>This module was invented and originally written in April
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync and gifted exclusively to the The Apache Group in July 1997
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <blockquote>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <a href="http://www.engelschall.com/"><code>Ralf S.
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync href="mailto:rse@engelschall.com"><code>rse@engelschall.com</code></a><br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync href="http://www.engelschall.com/"><code>www.engelschall.com</code></a>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync </blockquote>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <li><a href="#InternalRuleset">Ruleset Processing</a></li>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <li><a href="#InternalBackRefs">Regex Back-Reference
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <li><a href="#RewriteEngine">RewriteEngine</a></li>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <li><a href="#RewriteOptions">RewriteOptions</a></li>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <li><a href="#RewriteLogLevel">RewriteLogLevel</a></li>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <li><a href="#EnvVar">Environment Variables</a></li>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <li><a href="#Solutions">Practical Solutions</a></li>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <p>The internal processing of this module is very complex but
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync needs to be explained once even to the average user to avoid
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync common mistakes and to let you exploit its full
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync functionality.</p>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <p>First you have to understand that when Apache processes a
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync HTTP request it does this in phases. A hook for each of these
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync phases is provided by the Apache API. Mod_rewrite uses two of
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync these hooks: the URL-to-filename translation hook which is
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync used after the HTTP request has been read but before any
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync authorization starts and the Fixup hook which is triggered
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync after the authorization phases and after the per-directory
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync config files (<code>.htaccess</code>) have been read, but
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync before the content handler is activated.</p>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <p>So, after a request comes in and Apache has determined the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync corresponding server (or virtual server) the rewriting engine
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync starts processing of all mod_rewrite directives from the
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync per-server configuration in the URL-to-filename phase. A few
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync steps later when the final data directories are found, the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync per-directory configuration directives of mod_rewrite are
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync triggered in the Fixup phase. In both situations mod_rewrite
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rewrites URLs either to new URLs or to filenames, although
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync there is no obvious distinction between them. This is a usage
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync of the API which was not intended to be this way when the API
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync was designed, but as of Apache 1.x this is the only way
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync mod_rewrite can operate. To make this point more clear
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync remember the following two points:</p>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <li>Although mod_rewrite rewrites URLs to URLs, URLs to
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync filenames and even filenames to filenames, the API
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync currently provides only a URL-to-filename hook. In Apache
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync 2.0 the two missing hooks will be added to make the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync processing more clear. But this point has no drawbacks for
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync the user, it is just a fact which should be remembered:
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync Apache does more in the URL-to-filename hook than the API
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync intends for it.</li>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync Unbelievably mod_rewrite provides URL manipulations in
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <code>.htaccess</code> files, although these are reached
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync a very long time after the URLs have been translated to
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync filenames. It has to be this way because
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <code>.htaccess</code> files live in the filesystem, so
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync processing has already reached this stage. In other
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync words: According to the API phases at this time it is too
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync late for any URL manipulations. To overcome this chicken
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync and egg problem mod_rewrite uses a trick: When you
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync mod_rewrite first rewrites the filename back to its
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync corresponding URL (which is usually impossible, but see
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync the <code>RewriteBase</code> directive below for the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync trick to achieve this) and then initiates a new internal
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync sub-request with the new URL. This restarts processing of
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync the API phases.
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <p>Again mod_rewrite tries hard to make this complicated
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync step totally transparent to the user, but you should
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync remember here: While URL manipulations in per-server
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync context are really fast and efficient, per-directory
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rewrites are slow and inefficient due to this chicken and
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync egg problem. But on the other hand this is the only way
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync mod_rewrite can provide (locally restricted) URL
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync manipulations to the average user.</p>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <h2><a id="InternalRuleset" name="InternalRuleset">Ruleset
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync Now when mod_rewrite is triggered in these two API phases, it
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync reads the configured rulesets from its configuration
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync structure (which itself was either created on startup for
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync per-server context or during the directory walk of the Apache
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync kernel for per-directory context). Then the URL rewriting
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync engine is started with the contained ruleset (one or more
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rules together with their conditions). The operation of the
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync URL rewriting engine itself is exactly the same for both
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync configuration contexts. Only the final result processing is
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <p>The order of rules in the ruleset is important because the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rewriting engine processes them in a special (and not very
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync obvious) order. The rule is this: The rewriting engine loops
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync through the ruleset rule by rule (<code>RewriteRule</code>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync directives) and when a particular rule matches it optionally
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync loops through existing corresponding conditions
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync (<code>RewriteCond</code> directives). For historical reasons
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync the conditions are given first, and so the control flow is a
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync little bit long-winded. See Figure 1 for more details.</p>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync height="385"
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync alt="[Needs graphics capability to display]" /></td>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync control flow through the rewriting ruleset</td>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <p>As you can see, first the URL is matched against the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <em>Pattern</em> of each rule. When it fails mod_rewrite
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync immediately stops processing this rule and continues with the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync next rule. If the <em>Pattern</em> matches, mod_rewrite looks
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync for corresponding rule conditions. If none are present, it
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync just substitutes the URL with a new value which is
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync constructed from the string <em>Substitution</em> and goes on
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync with its rule-looping. But if conditions exist, it starts an
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync inner loop for processing them in the order that they are
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync listed. For conditions the logic is different: we don't match
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync a pattern against the current URL. Instead we first create a
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync back-references, map lookups, <em>etc.</em> and then we try
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync to match <em>CondPattern</em> against it. If the pattern
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync doesn't match, the complete set of conditions and the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync corresponding rule fails. If the pattern matches, then the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync next condition is processed until no more conditions are
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync available. If all conditions match, processing is continued
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync with the substitution of the URL with
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <p>As of Apache 1.3.20, special characters in
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <i>TestString</i> and <i>Substitution</i> strings can be
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync escaped (that is, treated as normal characters without their
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync usual special meaning) by prefixing them with a slosh ('\')
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync character. In other words, you can include an actual
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync dollar-sign character in a <i>Substitution</i> string by
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync using '<code>\$</code>'; this keeps mod_rewrite from trying
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync to treat it as a backreference.</p>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <h2><a id="InternalBackRefs" name="InternalBackRefs">Regex
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync One important thing here has to be remembered: Whenever you
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync use parentheses in <em>Pattern</em> or in one of the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <em>CondPattern</em>, back-references are internally created
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync which can be used with the strings <code>$N</code> and
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <code>%N</code> (see below). These are available for creating
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync the strings <em>Substitution</em> and <em>TestString</em>.
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync Figure 2 shows to which locations the back-references are
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync transfered for expansion.
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync height="179"
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync alt="[Needs graphics capability to display]" /></td>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync back-reference flow through a rule</td>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <p>We know this was a crash course on mod_rewrite's internal
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync processing. But you will benefit from this knowledge when
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync reading the following documentation of the available
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync directives.</p>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync name="Configuration">Configuration Directives</a></h1>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rel="Help"><strong>Syntax:</strong></a> RewriteEngine
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync on|off<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Default:</strong></a> <code>RewriteEngine
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Context:</strong></a> server config,
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync virtual host, directory, .htaccess<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Override:</strong></a> FileInfo<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Status:</strong></a> Extension<br />
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rel="Help"><strong>Module:</strong></a> mod_rewrite.c<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Compatibility:</strong></a> Apache
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <p>The <code>RewriteEngine</code> directive enables or
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync disables the runtime rewriting engine. If it is set to
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <code>off</code> this module does no runtime processing at
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync all. It does not even update the <code>SCRIPT_URx</code>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync environment variables.</p>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <p>Use this directive to disable the module instead of
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync directives!</p>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <p>Note that, by default, rewrite configurations are not
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync inherited. This means that you need to have a
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <code>RewriteEngine on</code> directive for each virtual host
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync in which you wish to use it.</p>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Syntax:</strong></a> RewriteOptions
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Default:</strong></a> <em>None</em><br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Context:</strong></a> server config,
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync virtual host, directory, .htaccess<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Override:</strong></a> FileInfo<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Status:</strong></a> Extension<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Module:</strong></a> mod_rewrite.c<br />
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rel="Help"><strong>Compatibility:</strong></a> Apache
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <p>The <code>RewriteOptions</code> directive sets some
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync special options for the current per-server or per-directory
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync configuration. The <em>Option</em> strings can be one of the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync following:</p>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync This forces the current configuration to inherit the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync configuration of the parent. In per-virtual-server context
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync this means that the maps, conditions and rules of the main
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync server are inherited. In per-directory context this means
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync that conditions and rules of the parent directory's
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <code>.htaccess</code> configuration are inherited.</li>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <h3><a id="RewriteLog" name="RewriteLog">RewriteLog</a></h3>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rel="Help"><strong>Default:</strong></a> <em>None</em><br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Context:</strong></a> server config,
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync virtual host<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Status:</strong></a> Extension<br />
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rel="Help"><strong>Module:</strong></a> mod_rewrite.c<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Compatibility:</strong></a> Apache
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <p>The <code>RewriteLog</code> directive sets the name of the
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync file to which the server logs any rewriting actions it
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync performs. If the name does not begin with a slash
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync ('<code>/</code>') then it is assumed to be relative to the
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <em>Server Root</em>. The directive should occur only once
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync per server config.</p>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <td><strong>Note</strong>: To disable the logging of
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rewriting actions it is not recommended to set
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <em>Filename</em> to <code>/dev/null</code>, because
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync although the rewriting engine does not then output to a
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync logfile it still creates the logfile output internally.
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <strong>This will slow down the server with no advantage
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync to the administrator!</strong> To disable logging either
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync directive or use <code>RewriteLogLevel 0</code>!</td>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync href="/misc/security_tips.html">Apache Security
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync Tips</a> document for details on why your security could
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync be compromised if the directory where logfiles are stored
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync is writable by anyone other than the user that starts the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync server.</td>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <blockquote>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync </blockquote>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Syntax:</strong></a> RewriteLogLevel
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Context:</strong></a> server config,
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync virtual host<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Status:</strong></a> Extension<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Module:</strong></a> mod_rewrite.c<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Compatibility:</strong></a> Apache
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <p>The <code>RewriteLogLevel</code> directive sets the
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync verbosity level of the rewriting logfile. The default level 0
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync means no logging, while 9 or more means that practically all
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync actions are logged.</p>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <p>To disable the logging of rewriting actions simply set
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <em>Level</em> to 0. This disables all rewrite action
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <td><strong>Notice:</strong> Using a high value for
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync dramatically! Use the rewriting logfile at a
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <em>Level</em> greater than 2 only for debugging!</td>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <blockquote>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsyncRewriteLogLevel 3
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync </blockquote>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Syntax:</strong></a> RewriteLock
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Default:</strong></a> <em>None</em><br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Context:</strong></a> server config<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Status:</strong></a> Extension<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Module:</strong></a> mod_rewrite.c<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Compatibility:</strong></a> Apache
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <p>This directive sets the filename for a synchronization
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync lockfile which mod_rewrite needs to communicate with
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <samp>RewriteMap</samp> <em>programs</em>. Set this lockfile
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync to a local path (not on a NFS-mounted device) when you want
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync to use a rewriting map-program. It is not required for other
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync types of rewriting maps.</p>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <h3><a id="RewriteMap" name="RewriteMap">RewriteMap</a></h3>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <em>MapName</em> <em>MapType</em>:<em>MapSource</em><br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Default:</strong></a> not used per
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync default<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Context:</strong></a> server config,
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync virtual host<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Status:</strong></a> Extension<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Module:</strong></a> mod_rewrite.c<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync rel="Help"><strong>Compatibility:</strong></a> Apache 1.2
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync (partially), Apache 1.3<br />
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <em>Rewriting Map</em> which can be used inside rule
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync substitution strings by the mapping-functions to
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync insert/substitute fields through a key lookup. The source of
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync this lookup can be of various types.</p>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <p>The <a id="mapfunc" name="mapfunc"><em>MapName</em></a> is
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync the name of the map and will be used to specify a
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync mapping-function for the substitution strings of a rewriting
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync rule via one of the following constructs:</p>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <strong><code>${</code> <em>MapName</em> <code>:</code>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <em>LookupKey</em> <code>|</code> <em>DefaultValue</em>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync </blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync When such a construct occurs the map <em>MapName</em> is
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync consulted and the key <em>LookupKey</em> is looked-up. If the
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync key is found, the map-function construct is substituted by
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <em>SubstValue</em>. If the key is not found then it is
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync substituted by <em>DefaultValue</em> or by the empty string
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <p>The following combinations for <em>MapType</em> and
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync MapType: <code>txt</code>, MapSource: Unix filesystem
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync path to valid regular file
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync <p>This is the standard rewriting map feature where the
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <em>MapSource</em> is a plain ASCII file containing
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync either blank lines, comment lines (starting with a '#'
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync character) or pairs like the following - one per
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync <blockquote>
1ca4a6ab3f30cc1d00058a1a23e62bb267016ea1vboxsync </blockquote>
798f4f8aae5ac9c2a1de841f2938b991c6d60ef5vboxsync bgcolor="#F0F0F0">
## map.txt -- rewriting map
Ralf.S.Engelschall rse # Bastard Operator From Hell
Mr.Joe.Average joe # Mr. Average
## map.txt -- rewriting map
# ...put here any transformations or lookups...
RewriteRule ^oldstuff\.html$ newstuff.html
RewriteRule ...some special stuff for any of these hosts...
RewriteRule ^/$ /homepage.max.html [L]
RewriteRule ^/$ /homepage.min.html [L]
RewriteRule ^/$ /homepage.std.html [L]
^/somepath(.*) /otherpath$1 [R] http://thishost/otherpath/pathinfo
^/somepath(.*) http://thishost/otherpath$1 [P] not supported, because silly!
^localpath(.*) otherpath$1 [R] http://thishost/somepath/otherpath/pathinfo
^localpath(.*) /otherpath$1 [R] http://thishost/otherpath/pathinfo
^localpath(.*) http://thishost/otherpath$1 [P] not supported, because silly!
SCRIPT_URI=http://en1.engelschall.com/u/rse/
mod_rewrite. <!--#include virtual="footer.html" -->