2N/AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 2N/A This file is generated from xml source: DO NOT EDIT 2N/AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 2N/A--><
title>mod_rewrite - Apache HTTP Server</
title><
link href="/style/manual.css" type="text/css" rel="stylesheet"/></
head><
body><
blockquote><
div align="center"><
img alt="[APACHE DOCUMENTATION]" src="/images/sub.gif"/><
h3>Apache HTTP Server Version 2.0</
h3></
div><
h1 align="center">Apache Module mod_rewrite</
h1><
table cellspacing="1" cellpadding="0" bgcolor="#cccccc"><
tr><
td><
table bgcolor="#ffffff"><
tr><
td valign="top"><
span class="help">Description:</
span></
td><
td><
description>Provides a rule-based rewriting engine to rewrite requested
2N/AURLs on the fly</
description></
td></
tr><
tr><
td><
a href="module-dict.html#Status" class="help">Status:</
a></
td><
td>Extension</
td></
tr><
tr><
td><
a href="module-dict.html#ModuleIdentifier" class="help">Module Identifier:</
a></
td><
td>rewrite_module</
td></
tr><
tr><
td align="left" valign="top"><
a href="module-dict.html#Compatibility" class="help">Compatibility:</
a></
td><
td><
compatibility>Available in Apache 1.3 and later</
compatibility></
td></
tr></
table></
td></
tr></
table><
h2>Summary</
h2><
summary>
2N/A <
em>``The great thing about mod_rewrite is it gives you
2N/A all the configurability and flexibility of Sendmail.
2N/A The downside to mod_rewrite is that it gives you all
2N/A the configurability and flexibility of Sendmail.''</
em>
2N/A -- Brian Behlendorf<
br>
2N/A <
em>`` Despite the tons of examples and docs,
2N/A mod_rewrite is voodoo. Damned cool voodoo, but still
2N/A <
p>Welcome to mod_rewrite, the Swiss Army Knife of URL
2N/A <
p>This module uses a rule-based rewriting engine (based on a
2N/A regular-expression parser) to rewrite requested URLs on the
2N/A fly. It supports an unlimited number of rules and an
2N/A unlimited number of attached rule conditions for each rule to
2N/A provide a really flexible and powerful URL manipulation
2N/A mechanism. The URL manipulations can depend on various tests,
2N/A for instance server variables, environment variables, HTTP
2N/A headers, time stamps and even external database lookups in
2N/A various formats can be used to achieve a really granular URL
2N/A <
p>This module operates on the full URLs (including the
2N/A path-info part) both in per-server context
2N/A (<
code>.htaccess</
code>) and can even generate query-string
2N/A parts on result. The rewritten result can lead to internal
2N/A sub-processing, external request redirection or even to an
2N/A internal proxy throughput.</
p>
2N/A <
p>But all this functionality and flexibility has its
2N/A drawback: complexity. So don't expect to understand this
2N/A entire module in just one day.</
p>
2N/A <
p>This module was invented and originally written in April
2N/A 1996 and gifted exclusively to the The Apache Group in July 1997
2N/A Engelschall</
code></
a><
br>
2N/A <
a href="mailto:rse@engelschall.com"><
code>rse@engelschall.com</
code></
a><
br>
2N/A</
summary><
h2>Directives</
h2><
ul><
li><
a href="#rewritebase">RewriteBase</
a></
li><
li><
a href="#rewritecond">RewriteCond</
a></
li><
li><
a href="#rewriteengine">RewriteEngine</
a></
li><
li><
a href="#rewritelock">RewriteLock</
a></
li><
li><
a href="#rewritelog">RewriteLog</
a></
li><
li><
a href="#rewriteloglevel">RewriteLogLevel</
a></
li><
li><
a href="#rewritemap">RewriteMap</
a></
li><
li><
a href="#rewriteoptions">RewriteOptions</
a></
li><
li><
a href="#rewriterule">RewriteRule</
a></
li></
ul><
h2><
a name="Internal">Interal Processing</
a></
h2>
2N/A <
p>The internal processing of this module is very complex but
2N/A needs to be explained once even to the average user to avoid
2N/A common mistakes and to let you exploit its full
2N/A<
h3><
a name="InternalAPI">API Phases</
a></
h3>
2N/A <
p>First you have to understand that when Apache processes a
2N/A HTTP request it does this in phases. A hook for each of these
2N/A phases is provided by the Apache API. Mod_rewrite uses two of
2N/A these hooks: the URL-to-filename translation hook which is
2N/A used after the HTTP request has been read but before any
2N/A authorization starts and the Fixup hook which is triggered
2N/A after the authorization phases and after the per-directory
2N/A config files (<
code>.htaccess</
code>) have been read, but
2N/A before the content handler is activated.</
p>
2N/A <
p>So, after a request comes in and Apache has determined the
2N/A corresponding server (or virtual server) the rewriting engine
2N/A starts processing of all mod_rewrite directives from the
2N/A per-server configuration in the URL-to-filename phase. A few
2N/A steps later when the final data directories are found, the
2N/A per-directory configuration directives of mod_rewrite are
2N/A triggered in the Fixup phase. In both situations mod_rewrite
2N/A rewrites URLs either to new URLs or to filenames, although
2N/A there is no obvious distinction between them. This is a usage
2N/A of the API which was not intended to be this way when the API
2N/A was designed, but as of Apache
1.x this is the only way
2N/A mod_rewrite can operate. To make this point more clear
2N/A remember the following two points:</
p>
2N/A <
li>Although mod_rewrite rewrites URLs to URLs, URLs to
2N/A filenames and even filenames to filenames, the API
2N/A currently provides only a URL-to-filename hook. In Apache
2N/A 2.0 the two missing hooks will be added to make the
2N/A processing more clear. But this point has no drawbacks for
2N/A the user, it is just a fact which should be remembered:
2N/A Apache does more in the URL-to-filename hook than the API
2N/A intends for it.</
li>
2N/A Unbelievably mod_rewrite provides URL manipulations in
2N/A per-directory context, <
em>
i.e.</
em>, within
2N/A <
code>.htaccess</
code> files, although these are reached
2N/A a very long time after the URLs have been translated to
2N/A filenames. It has to be this way because
2N/A <
code>.htaccess</
code> files live in the filesystem, so
2N/A processing has already reached this stage. In other
2N/A words: According to the API phases at this time it is too
2N/A late for any URL manipulations. To overcome this chicken
2N/A and egg problem mod_rewrite uses a trick: When you
2N/A mod_rewrite first rewrites the filename back to its
2N/A corresponding URL (which is usually impossible, but see
2N/A the <
code>RewriteBase</
code> directive below for the
2N/A trick to achieve this) and then initiates a new internal
2N/A sub-request with the new URL. This restarts processing of
2N/A <
p>Again mod_rewrite tries hard to make this complicated
2N/A step totally transparent to the user, but you should
2N/A remember here: While URL manipulations in per-server
2N/A context are really fast and efficient, per-directory
2N/A rewrites are slow and inefficient due to this chicken and
2N/A egg problem. But on the other hand this is the only way
2N/A mod_rewrite can provide (locally restricted) URL
2N/A manipulations to the average user.</
p>
2N/A <
p>Don't forget these two points!</
p>
2N/A<
h3><
a name="InternalRuleset">Ruleset Processing</
a></
h3>
2N/A <
p>Now when mod_rewrite is triggered in these two API phases, it
2N/A reads the configured rulesets from its configuration
2N/A structure (which itself was either created on startup for
2N/A per-server context or during the directory walk of the Apache
2N/A kernel for per-directory context). Then the URL rewriting
2N/A engine is started with the contained ruleset (one or more
2N/A rules together with their conditions). The operation of the
2N/A URL rewriting engine itself is exactly the same for both
2N/A configuration contexts. Only the final result processing is
2N/A <
p>The order of rules in the ruleset is important because the
2N/A rewriting engine processes them in a special (and not very
2N/A obvious) order. The rule is this: The rewriting engine loops
2N/A through the ruleset rule by rule (<
a href="#rewriterule" class="directive"><
code class="directive">RewriteRule</
code></
a> directives) and
2N/A when a particular rule matches it optionally loops through
2N/A existing corresponding conditions (<
code>RewriteCond</
code>
2N/A directives). For historical reasons the conditions are given
2N/A first, and so the control flow is a little bit long-winded. See
2N/A Figure 1 for more details.</
p>
2N/A <
div align="CENTER">
2N/A <
table cellspacing="0" cellpadding="2" border="0">
2N/A <
td align="CENTER"><
strong>Figure 1:</
strong> The
2N/A control flow through the rewriting ruleset</
td>
2N/A <
p>As you can see, first the URL is matched against the
2N/A <
em>Pattern</
em> of each rule. When it fails mod_rewrite
2N/A immediately stops processing this rule and continues with the
2N/A next rule. If the <
em>Pattern</
em> matches, mod_rewrite looks
2N/A for corresponding rule conditions. If none are present, it
2N/A just substitutes the URL with a new value which is
2N/A constructed from the string <
em>Substitution</
em> and goes on
2N/A with its rule-looping. But if conditions exist, it starts an
2N/A inner loop for processing them in the order that they are
2N/A listed. For conditions the logic is different: we don't match
2N/A a pattern against the current URL. Instead we first create a
2N/A string <
em>TestString</
em> by expanding variables,
2N/A back-references, map lookups, <
em>etc.</
em> and then we try
2N/A to match <
em>CondPattern</
em> against it. If the pattern
2N/A doesn't match, the complete set of conditions and the
2N/A corresponding rule fails. If the pattern matches, then the
2N/A next condition is processed until no more conditions are
2N/A available. If all conditions match, processing is continued
2N/A with the substitution of the URL with
2N/A <
em>Substitution</
em>.</
p>
2N/A<
h3><
a name="quoting">Quoting Special Characters</
a></
h3>
2N/A <
p>As of Apache 1.3.20, special characters in
2N/A <
i>TestString</
i> and <
i>Substitution</
i> strings can be
2N/A escaped (that is, treated as normal characters without their
2N/A usual special meaning) by prefixing them with a slosh ('\')
2N/A character. In other words, you can include an actual
2N/A dollar-sign character in a <
i>Substitution</
i> string by
2N/A using '<
code>\$</
code>'; this keeps mod_rewrite from trying
2N/A to treat it as a backreference.</
p>
2N/A<
h3><
a name="InternalBackRefs">Regex Back-Reference Availability</
a></
h3>
2N/A <
p>One important thing here has to be remembered: Whenever you
2N/A use parentheses in <
em>Pattern</
em> or in one of the
2N/A <
em>CondPattern</
em>, back-references are internally created
2N/A which can be used with the strings <
code>$N</
code> and
2N/A <
code>%N</
code> (see below). These are available for creating
2N/A the strings <
em>Substitution</
em> and <
em>TestString</
em>.
2N/A Figure 2 shows to which locations the back-references are
2N/A transfered for expansion.</
p>
2N/A <
div align="CENTER">
2N/A <
table cellspacing="0" cellpadding="2" border="0">
2N/A <
td align="CENTER"><
strong>Figure 2:</
strong> The
2N/A back-reference flow through a rule</
td>
2N/A <
p>We know this was a crash course on mod_rewrite's internal
2N/A processing. But you will benefit from this knowledge when
2N/A reading the following documentation of the available
2N/A<
h2><
a name="EnvVar">Environment Variables</
a></
h2>
2N/A <
p>This module keeps track of two additional (non-standard)
2N/A CGI/
SSI environment variables named <
code>SCRIPT_URL</
code>
2N/A and <
code>SCRIPT_URI</
code>. These contain the
2N/A <
em>logical</
em> Web-view to the current resource, while the
2N/A standard
CGI/
SSI variables <
code>SCRIPT_NAME</
code> and
2N/A <
code>SCRIPT_FILENAME</
code> contain the <
em>physical</
em>
2N/A <
p>Notice: These variables hold the
URI/
URL <
em>as they were
2N/A initially requested</
em>, <
em>
i.e.</
em>, <
em>before</
em> any
2N/A rewriting. This is important because the rewriting process is
2N/A primarily used to rewrite logical URLs to physical
2N/A <
p><
strong>Example:</
strong></
p>
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A<
h2><
a name="Solutions">Practical Solutions</
a></
h2>
2N/A Rewriting Guide</
a> available, which provides a collection of
2N/A practical solutions for URL-based problems. There you can
2N/A find real-life rulesets and additional information about
2N/A<
hr/><
h2><
a name="RewriteBase">RewriteBase</
a> <
a name="rewritebase">Directive</
a></
h2><
table cellpadding="1" cellspacing="0" border="0" bgcolor="#cccccc"><
tr><
td><
table bgcolor="#ffffff"><
tr><
td><
strong>Description: </
strong></
td><
td>Sets the base URL for per-directory rewrites</
td></
tr><
tr><
td><
a href="directive-dict.html#Syntax" class="help">Syntax:</
a></
td><
td><
syntax>RewriteBase <
em>URL-path</
em></
syntax></
td></
tr><
tr><
td><
a href="directive-dict.html#Default" class="help">Default:</
a></
td><
td><
code>RewriteBase physical-directory-path</
code></
td></
tr><
tr><
td><
a href="directive-dict.html#Context" class="help">Context:</
a></
td><
td>directory, .htaccess</
td></
tr><
tr><
td><
a href="directive-dict.html#Override" class="help">Override:</
a></
td><
td>FileInfo</
td></
tr><
tr><
td><
a href="directive-dict.html#Status" class="help">Status:</
a></
td><
td>Extension</
td></
tr><
tr><
td><
a href="directive-dict.html#Module" class="help">Module:</
a></
td><
td>mod_rewrite</
td></
tr></
table></
td></
tr></
table><
usage>
2N/A <
p>The <
code class="directive">RewriteBase</
code> directive explicitly
2N/A sets the base URL for per-directory rewrites. As you will see
2N/A below, <
a href="#rewriterule" class="directive"><
code class="directive">RewriteRule</
code></
a>
2N/A can be used in per-directory config files
2N/A (<
code>.htaccess</
code>). There it will act locally,
2N/A <
em>
i.e.</
em>, the local directory prefix is stripped at this
2N/A stage of processing and your rewriting rules act only on the
2N/A remainder. At the end it is automatically added back to the
2N/A <
p>When a substitution occurs for a new URL, this module has
2N/A to re-inject the URL into the server processing. To be able
2N/A to do this it needs to know what the corresponding URL-prefix
2N/A or URL-base is. By default this prefix is the corresponding
2N/A filepath itself. <
strong>But at most websites URLs are NOT
2N/A directly related to physical filename paths, so this
2N/A assumption will usually be wrong!</
strong> There you have to
2N/A use the <
code>RewriteBase</
code> directive to specify the
2N/A correct URL-prefix.</
p>
2N/A<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"> If your webserver's URLs are <
strong>not</
strong> directly
2N/Arelated to physical file paths, you have to use
2N/A<
code class="directive">RewriteBase</
code> in every <
code>.htaccess</
code>
2N/Afiles where you want to use <
a href="#rewriterule" class="directive"><
code class="directive">RewriteRule</
code></
a> directives.
2N/A</
td></
tr></
table></
blockquote>
2N/A <
p> For example, assume the following per-directory config file:</
p>
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
2N/A# Remember: /
abc/
def is the physical path of /xyz, <
em>
i.e.</
em>, the server
2N/A# let the server know that we were reached via /xyz and not
2N/A# now the rewriting rules
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A <
p>In the above example, a request to
2N/A<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"><
p align="center"><
strong>For Apache Hackers</
strong></
p>
2N/A<
p>The following list gives detailed information about
2N/A the internal processing steps:</
p>
2N/A<
font size="-1">Request:
2N/A <
p><
font size="-1">This seems very complicated but is
2N/A the correct Apache internal processing, because the
2N/A per-directory rewriting comes too late in the
2N/A process. So, when it occurs the (rewritten) request
2N/A has to be re-injected into the Apache kernel! BUT:
2N/A While this seems like a serious overhead, it really
2N/A isn't, because this re-injection happens fully
2N/A internally to the Apache server and the same
2N/A procedure is used by many other operations inside
2N/A Apache. So, you can be sure the design and
2N/A implementation is correct.</
font></
p>
2N/A</
td></
tr></
table></
blockquote>
2N/A</
usage><
hr/><
h2><
a name="RewriteCond">RewriteCond</
a> <
a name="rewritecond">Directive</
a></
h2><
table cellpadding="1" cellspacing="0" border="0" bgcolor="#cccccc"><
tr><
td><
table bgcolor="#ffffff"><
tr><
td><
strong>Description: </
strong></
td><
td>Defines a condition under which rewriting will take place
2N/A <
em>TestString</
em> <
em>CondPattern</
em></
syntax></
td></
tr><
tr><
td><
a href="directive-dict.html#Default" class="help">Default:</
a></
td><
td><
code>None</
code></
td></
tr><
tr><
td><
a href="directive-dict.html#Context" class="help">Context:</
a></
td><
td>server config, virtual host, directory, .htaccess</
td></
tr><
tr><
td><
a href="directive-dict.html#Override" class="help">Override:</
a></
td><
td>FileInfo</
td></
tr><
tr><
td><
a href="directive-dict.html#Status" class="help">Status:</
a></
td><
td>Extension</
td></
tr><
tr><
td><
a href="directive-dict.html#Module" class="help">Module:</
a></
td><
td>mod_rewrite</
td></
tr></
table></
td></
tr></
table><
usage>
2N/A <
p>The <
code class="directive">RewriteCond</
code> directive defines a
2N/A rule condition. Precede a <
a href="#rewriterule" class="directive"><
code class="directive">RewriteRule</
code></
a> directive with one
2N/A or more <
code class="directive">RewriteCond</
code> directives. The following
2N/A rewriting rule is only used if its pattern matches the current
2N/A state of the URI <
strong>and</
strong> if these additional
2N/A conditions apply too.</
p>
2N/A <
p><
em>TestString</
em> is a string which can contains the
2N/A following expanded constructs in addition to plain text:</
p>
2N/A <
strong>RewriteRule backreferences</
strong>: These are
2N/A backreferences of the form
2N/A <
strong><
code>$N</
code></
strong>
2N/A (0 <= N <= 9) which provide access to the grouped
2N/A parts (parenthesis!) of the pattern from the
2N/A corresponding <
code>RewriteRule</
code> directive (the one
2N/A following the current bunch of <
code>RewriteCond</
code>
2N/A <
strong>RewriteCond backreferences</
strong>: These are
2N/A backreferences of the form
2N/A <
strong><
code>%N</
code></
strong>
2N/A (1 <= N <= 9) which provide access to the grouped
2N/A parts (parentheses!) of the pattern from the last matched
2N/A <
code>RewriteCond</
code> directive in the current bunch
2N/A <
strong>RewriteMap expansions</
strong>: These are
2N/A expansions of the form
2N/A <
strong><
code>${mapname:key|default}</
code></
strong>
2N/A See <
a href="#mapfunc">the documentation for
2N/A RewriteMap</
a> for more details.
2N/A <
strong>Server-Variables</
strong>: These are variables of
2N/A <
strong><
code>%{</
code> <
em>NAME_OF_VARIABLE</
em>
2N/A <
code>}</
code></
strong>
2N/A where <
em>NAME_OF_VARIABLE</
em> can be a string taken
2N/A from the following list:
2N/A <
table bgcolor="#F0F0F0" cellspacing="0" cellpadding="5">
2N/A <
strong>HTTP headers:</
strong>
2N/A <
p><
font size="-1">HTTP_USER_AGENT<
br>
2N/A HTTP_PROXY_CONNECTION<
br>
2N/A <
strong>connection & request:</
strong>
2N/A <
p><
font size="-1">REMOTE_ADDR<
br>
2N/A <
strong>server internals:</
strong>
2N/A <
p><
font size="-1">DOCUMENT_ROOT<
br>
2N/A <
strong>system stuff:</
strong>
2N/A <
p><
font size="-1">TIME_YEAR<
br>
2N/A <
strong>specials:</
strong>
2N/A <
p><
font size="-1">API_VERSION<
br>
2N/A REQUEST_FILENAME<
br>
2N/A<
blockquote><
table><
tr><
td bgcolor="#e0e5f5">
2N/A <
p>These variables all
2N/A correspond to the similarly named HTTP
2N/A MIME-headers, C variables of the Apache server or
2N/A <
code>struct tm</
code> fields of the Unix system.
2N/A Most are documented elsewhere in the Manual or in
2N/A the CGI specification. Those that are special to
2N/A mod_rewrite include:</
p>
2N/A <
dt><
code>IS_SUBREQ</
code></
dt>
2N/A <
dd>Will contain the text "true" if the request
2N/A currently being processed is a sub-request,
2N/A "false" otherwise. Sub-requests may be generated
2N/A by modules that need to resolve additional files
2N/A or URIs in order to complete their tasks.</
dd>
2N/A <
dt><
code>API_VERSION</
code></
dt>
2N/A <
dd>This is the version of the Apache module API
2N/A (the internal interface between server and
2N/A module) in the current httpd build, as defined in
2N/A corresponds to the version of Apache in use (in
2N/A the release version of Apache 1.3.14, for
2N/A instance, it is 19990320:10), but is mainly of
2N/A interest to module authors.</
dd>
2N/A <
dt><
code>THE_REQUEST</
code></
dt>
2N/A <
dd>The full HTTP request line sent by the
2N/A browser to the server (
e.g., "<
code>GET
2N/A include any additional headers sent by the
2N/A <
dt><
code>REQUEST_URI</
code></
dt>
2N/A <
dd>The resource requested in the HTTP request
2N/A line. (In the example above, this would be
2N/A <
dt><
code>REQUEST_FILENAME</
code></
dt>
2N/A <
dd>The full local filesystem path to the file or
2N/A script matching the request.</
dd>
2N/A</
td></
tr></
table></
blockquote>
2N/A <
p>Special Notes:</
p>
2N/A <
li>The variables SCRIPT_FILENAME and REQUEST_FILENAME
2N/A contain the same value, <
em>
i.e.</
em>, the value of the
2N/A <
code>filename</
code> field of the internal
2N/A <
code>request_rec</
code> structure of the Apache server.
2N/A The first name is just the commonly known CGI variable name
2N/A while the second is the consistent counterpart to
2N/A REQUEST_URI (which contains the value of the
2N/A <
code>uri</
code> field of <
code>request_rec</
code>).</
li>
2N/A <
li>There is the special format:
2N/A <
code>%{ENV:variable}</
code> where <
em>variable</
em> can be
2N/A any environment variable. This is looked-up via internal
2N/A Apache structures and (if not found there) via
2N/A <
code>getenv()</
code> from the Apache server process.</
li>
2N/A <
li>There is the special format:
2N/A <
code>%{HTTP:header}</
code> where <
em>header</
em> can be
2N/A any HTTP MIME-header name. This is looked-up from the HTTP
2N/A request. Example: <
code>%{HTTP:Proxy-Connection}</
code> is
2N/A the value of the HTTP header
2N/A ``<
code>Proxy-Connection:</
code>''.</
li>
2N/A <
li>There is the special format
2N/A <
code>%{LA-U:variable}</
code> for look-aheads which perform
2N/A an internal (URL-based) sub-request to determine the final
2N/A value of <
em>variable</
em>. Use this when you want to use a
2N/A variable for rewriting which is actually set later in an
2N/A API phase and thus is not available at the current stage.
2N/A For instance when you want to rewrite according to the
2N/A <
code>REMOTE_USER</
code> variable from within the
2N/A to use <
code>%{LA-U:REMOTE_USER}</
code> because this
2N/A variable is set by the authorization phases which come
2N/A <
em>after</
em> the URL translation phase where mod_rewrite
2N/A operates. On the other hand, because mod_rewrite implements
2N/A its per-directory context (<
code>.htaccess</
code> file) via
2N/A the Fixup phase of the API and because the authorization
2N/A phases come <
em>before</
em> this phase, you just can use
2N/A <
code>%{REMOTE_USER}</
code> there.</
li>
2N/A <
li>There is the special format:
2N/A <
code>%{LA-F:variable}</
code> which performs an internal
2N/A (filename-based) sub-request to determine the final value
2N/A of <
em>variable</
em>. Most of the time this is the same as
2N/A <
p><
em>CondPattern</
em> is the condition pattern,
2N/A <
em>
i.e.</
em>, a regular expression which is applied to the
2N/A current instance of the <
em>TestString</
em>, <
em>
i.e.</
em>,
2N/A <
em>TestString</
em> is evaluated and then matched against
2N/A <
em>CondPattern</
em>.</
p>
2N/A <
p><
strong>Remember:</
strong> <
em>CondPattern</
em> is a
2N/A standard <
em>Extended Regular Expression</
em> with some
2N/A <
li>You can prefix the pattern string with a
2N/A '<
code>!</
code>' character (exclamation mark) to specify a
2N/A <
strong>non</
strong>-matching pattern.</
li>
2N/A There are some special variants of <
em>CondPatterns</
em>.
2N/A Instead of real regular expression strings you can also
2N/A use one of the following:
2N/A <
li>'<
strong><CondPattern</
strong>' (is lexically
2N/A Treats the <
em>CondPattern</
em> as a plain string and
2N/A compares it lexically to <
em>TestString</
em>. True if
2N/A <
em>TestString</
em> is lexically lower than
2N/A <
em>CondPattern</
em>.</
li>
2N/A <
li>'<
strong>>CondPattern</
strong>' (is lexically
2N/A Treats the <
em>CondPattern</
em> as a plain string and
2N/A compares it lexically to <
em>TestString</
em>. True if
2N/A <
em>TestString</
em> is lexically greater than
2N/A <
em>CondPattern</
em>.</
li>
2N/A <
li>'<
strong>=CondPattern</
strong>' (is lexically
2N/A Treats the <
em>CondPattern</
em> as a plain string and
2N/A compares it lexically to <
em>TestString</
em>. True if
2N/A <
em>TestString</
em> is lexically equal to
2N/A <
em>CondPattern</
em>,
i.e the two strings are exactly
2N/A equal (character by character). If <
em>CondPattern</
em>
2N/A is just <
samp>""</
samp> (two quotation marks) this
2N/A compares <
em>TestString</
em> to the empty string.</
li>
2N/A <
li>'<
strong>-d</
strong>' (is
2N/A <
strong>d</
strong>irectory)<
br>
2N/A Treats the <
em>TestString</
em> as a pathname and tests
2N/A if it exists and is a directory.</
li>
2N/A <
li>'<
strong>-f</
strong>' (is regular
2N/A <
strong>f</
strong>ile)<
br>
2N/A Treats the <
em>TestString</
em> as a pathname and tests
2N/A if it exists and is a regular file.</
li>
2N/A <
li>'<
strong>-s</
strong>' (is regular file with
2N/A <
strong>s</
strong>ize)<
br>
2N/A Treats the <
em>TestString</
em> as a pathname and tests
2N/A if it exists and is a regular file with size greater
2N/A <
li>'<
strong>-l</
strong>' (is symbolic
2N/A <
strong>l</
strong>ink)<
br>
2N/A Treats the <
em>TestString</
em> as a pathname and tests
2N/A if it exists and is a symbolic link.</
li>
2N/A <
li>'<
strong>-F</
strong>' (is existing file via
2N/A Checks if <
em>TestString</
em> is a valid file and
2N/A accessible via all the server's currently-configured
2N/A access controls for that path. This uses an internal
2N/A subrequest to determine the check, so use it with care
2N/A because it decreases your servers performance!</
li>
2N/A <
li>'<
strong>-U</
strong>' (is existing URL via
2N/A Checks if <
em>TestString</
em> is a valid URL and
2N/A accessible via all the server's currently-configured
2N/A access controls for that path. This uses an internal
2N/A subrequest to determine the check, so use it with care
2N/A because it decreases your server's performance!</
li>
2N/A<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"><
p align="center"><
strong>Notice</
strong></
p>
2N/A All of these tests can
2N/A also be prefixed by an exclamation mark ('!') to
2N/A negate their meaning.
2N/A</
td></
tr></
table></
blockquote>
2N/A <
p>Additionally you can set special flags for
2N/A <
em>CondPattern</
em> by appending</
p>
2N/A <
strong><
code>[</
code><
em>flags</
em><
code>]</
code></
strong>
2N/A as the third argument to the <
code>RewriteCond</
code>
2N/A directive. <
em>Flags</
em> is a comma-separated list of the
2N/A <
li>'<
strong><
code>nocase|NC</
code></
strong>'
2N/A (<
strong>n</
strong>o <
strong>c</
strong>ase)<
br>
2N/A This makes the test case-insensitive, <
em>
i.e.</
em>, there
2N/A is no difference between 'A-Z' and 'a-z' both in the
2N/A expanded <
em>TestString</
em> and the <
em>CondPattern</
em>.
2N/A This flag is effective only for comparisons between
2N/A <
em>TestString</
em> and <
em>CondPattern</
em>. It has no
2N/A effect on filesystem and subrequest checks.</
li>
2N/A '<
strong><
code>ornext|OR</
code></
strong>'
2N/A (<
strong>or</
strong> next condition)<
br>
2N/A Use this to combine rule conditions with a local OR
2N/A instead of the implicit AND. Typical example:
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
2N/ARewriteCond %{REMOTE_HOST} ^host1.* [OR]
2N/ARewriteCond %{REMOTE_HOST} ^host2.* [OR]
2N/ARewriteCond %{REMOTE_HOST} ^host3.*
2N/ARewriteRule
...some special stuff for any of these hosts...
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A Without this flag you would have to write the
cond/
rule 2N/A <
p><
strong>Example:</
strong></
p>
2N/A <
p>To rewrite the Homepage of a site according to the
2N/A ``<
code>User-Agent:</
code>'' header of the request, you can
2N/A use the following: </
p>
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
2N/ARewriteCond %{HTTP_USER_AGENT} ^Mozilla.*
2N/ARewriteCond %{HTTP_USER_AGENT} ^Lynx.*
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A <
p>Interpretation: If you use Netscape Navigator as your
2N/A browser (which identifies itself as 'Mozilla'), then you
2N/A get the max homepage, which includes Frames, <
em>etc.</
em>
2N/A If you use the Lynx browser (which is Terminal-based), then
2N/A you get the min homepage, which contains no images, no
2N/A tables, <
em>etc.</
em> If you use any other browser you get
2N/A the standard homepage.</
p>
2N/A</
usage><
hr/><
h2><
a name="RewriteEngine">RewriteEngine</
a> <
a name="rewriteengine">Directive</
a></
h2><
table cellpadding="1" cellspacing="0" border="0" bgcolor="#cccccc"><
tr><
td><
table bgcolor="#ffffff"><
tr><
td><
strong>Description: </
strong></
td><
td/></
tr><
tr><
td><
a href="directive-dict.html#Syntax" class="help">Syntax:</
a></
td><
td><
syntax>RewriteEngine on|off</
syntax></
td></
tr><
tr><
td><
a href="directive-dict.html#Default" class="help">Default:</
a></
td><
td><
code>RewriteEngine off</
code></
td></
tr><
tr><
td><
a href="directive-dict.html#Context" class="help">Context:</
a></
td><
td>server config, virtual host, directory, .htaccess</
td></
tr><
tr><
td><
a href="directive-dict.html#Override" class="help">Override:</
a></
td><
td>FileInfo</
td></
tr><
tr><
td><
a href="directive-dict.html#Status" class="help">Status:</
a></
td><
td>Extension</
td></
tr><
tr><
td><
a href="directive-dict.html#Module" class="help">Module:</
a></
td><
td>mod_rewrite</
td></
tr></
table></
td></
tr></
table><
usage>
2N/A <
p>The <
code class="directive">RewriteEngine</
code> directive enables or
2N/A disables the runtime rewriting engine. If it is set to
2N/A <
code>off</
code> this module does no runtime processing at
2N/A all. It does not even update the <
code>SCRIPT_URx</
code>
2N/A environment variables.</
p>
2N/A <
p>Use this directive to disable the module instead of
2N/A commenting out all the <
a href="#rewriterule" class="directive"><
code class="directive">RewriteRule</
code></
a> directives!</
p>
2N/A <
p>Note that, by default, rewrite configurations are not
2N/A inherited. This means that you need to have a
2N/A <
code>RewriteEngine on</
code> directive for each virtual host
2N/A in which you wish to use it.</
p>
2N/A</
usage><
hr/><
h2><
a name="RewriteLock">RewriteLock</
a> <
a name="rewritelock">Directive</
a></
h2><
table cellpadding="1" cellspacing="0" border="0" bgcolor="#cccccc"><
tr><
td><
table bgcolor="#ffffff"><
tr><
td><
strong>Description: </
strong></
td><
td>Sets the name of the lock file used for RewriteMap
2N/Asynchronization</
td></
tr><
tr><
td><
a href="directive-dict.html#Syntax" class="help">Syntax:</
a></
td><
td><
syntax>RewriteLock <
em>file-path</
em></
syntax></
td></
tr><
tr><
td><
a href="directive-dict.html#Default" class="help">Default:</
a></
td><
td><
code>None</
code></
td></
tr><
tr><
td><
a href="directive-dict.html#Context" class="help">Context:</
a></
td><
td>server config</
td></
tr><
tr><
td><
a href="directive-dict.html#Status" class="help">Status:</
a></
td><
td>Extension</
td></
tr><
tr><
td><
a href="directive-dict.html#Module" class="help">Module:</
a></
td><
td>mod_rewrite</
td></
tr></
table></
td></
tr></
table><
usage>
2N/A <
p>This directive sets the filename for a synchronization
2N/A lockfile which mod_rewrite needs to communicate with <
a href="#rewritemap" class="directive"><
code class="directive">RewriteMap</
code></
a>
2N/A <
em>programs</
em>. Set this lockfile to a local path (not on a
2N/A NFS-mounted device) when you want to use a rewriting
2N/A map-program. It is not required for other types of rewriting
2N/A</
usage><
hr/><
h2><
a name="RewriteLog">RewriteLog</
a> <
a name="rewritelog">Directive</
a></
h2><
table cellpadding="1" cellspacing="0" border="0" bgcolor="#cccccc"><
tr><
td><
table bgcolor="#ffffff"><
tr><
td><
strong>Description: </
strong></
td><
td>Sets the name of the file used for logging rewrite engine
2N/Aprocessing</
td></
tr><
tr><
td><
a href="directive-dict.html#Syntax" class="help">Syntax:</
a></
td><
td><
syntax>RewriteLog <
em>file-path</
em></
syntax></
td></
tr><
tr><
td><
a href="directive-dict.html#Context" class="help">Context:</
a></
td><
td>server config, virtual host</
td></
tr><
tr><
td><
a href="directive-dict.html#Status" class="help">Status:</
a></
td><
td>Extension</
td></
tr><
tr><
td><
a href="directive-dict.html#Module" class="help">Module:</
a></
td><
td>mod_rewrite</
td></
tr></
table></
td></
tr></
table><
usage>
2N/A <
p>The <
code class="directive">RewriteLog</
code> directive sets the name
2N/A of the file to which the server logs any rewriting actions it
2N/A performs. If the name does not begin with a slash
2N/A ('<
code>/</
code>') then it is assumed to be relative to the
2N/A <
em>Server Root</
em>. The directive should occur only once per
2N/A<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"> To disable the logging of
2N/A rewriting actions it is not recommended to set
2N/A <
em>Filename</
em> to <
code>/
dev/
null</
code>, because
2N/A although the rewriting engine does not then output to a
2N/A logfile it still creates the logfile output internally.
2N/A <
strong>This will slow down the server with no advantage
2N/A to the administrator!</
strong> To disable logging either
2N/A remove or comment out the <
code class="directive">RewriteLog</
code>
2N/A directive or use <
code>RewriteLogLevel 0</
code>!
2N/A</
td></
tr></
table></
blockquote>
2N/A<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"><
p align="center"><
strong>Security</
strong></
p>
2N/Adocument for details on why your security could be compromised if the
2N/Adirectory where logfiles are stored is writable by anyone other than
2N/Athe user that starts the server.
2N/A</
td></
tr></
table></
blockquote>
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
p align="center"><
strong>Example</
strong></
p><
code>
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A</
usage><
hr/><
h2><
a name="RewriteLogLevel">RewriteLogLevel</
a> <
a name="rewriteloglevel">Directive</
a></
h2><
table cellpadding="1" cellspacing="0" border="0" bgcolor="#cccccc"><
tr><
td><
table bgcolor="#ffffff"><
tr><
td><
strong>Description: </
strong></
td><
td>Sets the verbosity of the log file used by the rewrite
2N/Aengine</
td></
tr><
tr><
td><
a href="directive-dict.html#Syntax" class="help">Syntax:</
a></
td><
td><
syntax>RewriteLogLevel <
em>Level</
em></
syntax></
td></
tr><
tr><
td><
a href="directive-dict.html#Default" class="help">Default:</
a></
td><
td><
code>RerwiteLogLevel 0</
code></
td></
tr><
tr><
td><
a href="directive-dict.html#Context" class="help">Context:</
a></
td><
td>server config, virtual host</
td></
tr><
tr><
td><
a href="directive-dict.html#Status" class="help">Status:</
a></
td><
td>Extension</
td></
tr><
tr><
td><
a href="directive-dict.html#Module" class="help">Module:</
a></
td><
td>mod_rewrite</
td></
tr></
table></
td></
tr></
table><
usage>
2N/A <
p>The <
code class="directive">RewriteLogLevel</
code> directive sets the
2N/A verbosity level of the rewriting logfile. The default level 0
2N/A means no logging, while 9 or more means that practically all
2N/A actions are logged.</
p>
2N/A <
p>To disable the logging of rewriting actions simply set
2N/A <
em>Level</
em> to 0. This disables all rewrite action
2N/A<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"> Using a high value for
2N/A <
em>Level</
em> will slow down your Apache server
2N/A dramatically! Use the rewriting logfile at a
2N/A <
em>Level</
em> greater than 2 only for debugging!
2N/A</
td></
tr></
table></
blockquote>
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
p align="center"><
strong>Example</
strong></
p><
code>
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A</
usage><
hr/><
h2><
a name="RewriteMap">RewriteMap</
a> <
a name="rewritemap">Directive</
a></
h2><
table cellpadding="1" cellspacing="0" border="0" bgcolor="#cccccc"><
tr><
td><
table bgcolor="#ffffff"><
tr><
td><
strong>Description: </
strong></
td><
td>Defines a mapping function for key-lookup</
td></
tr><
tr><
td><
a href="directive-dict.html#Syntax" class="help">Syntax:</
a></
td><
td><
syntax>RewriteMap <
em>MapName</
em> <
em>MapType</
em>:<
em>MapSource</
em>
2N/A</
syntax></
td></
tr><
tr><
td><
a href="directive-dict.html#Default" class="help">Default:</
a></
td><
td><
code>None</
code></
td></
tr><
tr><
td><
a href="directive-dict.html#Context" class="help">Context:</
a></
td><
td>server config, virtual host</
td></
tr><
tr><
td><
a href="directive-dict.html#Status" class="help">Status:</
a></
td><
td>Extension</
td></
tr><
tr><
td><
a href="directive-dict.html#Module" class="help">Module:</
a></
td><
td>mod_rewrite</
td></
tr></
table></
td></
tr></
table><
usage>
2N/A <
p>The <
code class="directive">RewriteMap</
code> directive defines a
2N/A <
em>Rewriting Map</
em> which can be used inside rule
2N/A substitution strings by the mapping-functions to
2N/A this lookup can be of various types.</
p>
2N/A <
p>The <
a id="mapfunc" name="mapfunc"><
em>MapName</
em></
a> is
2N/A the name of the map and will be used to specify a
2N/A mapping-function for the substitution strings of a rewriting
2N/A rule via one of the following constructs:</
p>
2N/A <
strong><
code>${</
code> <
em>MapName</
em> <
code>:</
code>
2N/A <
em>LookupKey</
em> <
code>}</
code><
br>
2N/A <
code>${</
code> <
em>MapName</
em> <
code>:</
code>
2N/A <
em>LookupKey</
em> <
code>|</
code> <
em>DefaultValue</
em>
2N/A <
code>}</
code></
strong>
2N/A <
p>When such a construct occurs the map <
em>MapName</
em> is
2N/A consulted and the key <
em>LookupKey</
em> is looked-up. If the
2N/A key is found, the map-function construct is substituted by
2N/A <
em>SubstValue</
em>. If the key is not found then it is
2N/A substituted by <
em>DefaultValue</
em> or by the empty string
2N/A if no <
em>DefaultValue</
em> was specified.</
p>
2N/A <
p>The following combinations for <
em>MapType</
em> and
2N/A <
em>MapSource</
em> can be used:</
p>
2N/A <
strong>Standard Plain Text</
strong><
br>
2N/A MapType: <
code>txt</
code>, MapSource: Unix filesystem
2N/A path to valid regular file
2N/A <
p>This is the standard rewriting map feature where the
2N/A <
em>MapSource</
em> is a plain ASCII file containing
2N/A either blank lines, comment lines (starting with a '#'
2N/A character) or pairs like the following - one per
2N/A <
strong><
em>MatchingKey</
em>
2N/A <
em>SubstValue</
em></
strong>
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
p align="center"><
strong>Example</
strong></
p><
code>
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A <
strong>Randomized Plain Text</
strong><
br>
2N/A MapType: <
code>rnd</
code>, MapSource: Unix filesystem
2N/A path to valid regular file
2N/A <
p>This is identical to the Standard Plain Text variant
2N/A above but with a special post-processing feature: After
2N/A looking up a value it is parsed according to contained
2N/A ``<
code>|</
code>'' characters which have the meaning of
2N/A ``or''. In other words they indicate a set of
2N/A alternatives from which the actual returned value is
2N/A chosen randomly. Although this sounds crazy and useless,
2N/A it was actually designed for load balancing in a reverse
2N/A proxy situation where the looked up values are server
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
2N/Astatic www1|www2|www3|www4
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A <
strong>Hash File</
strong><
br>
2N/A MapType: <
code>dbm</
code>, MapSource: Unix filesystem
2N/A path to valid regular file
2N/A <
p>Here the source is a binary NDBM format file
2N/A containing the same contents as a <
em>Plain Text</
em>
2N/A format file, but in a special representation which is
2N/A optimized for really fast lookups. You can create such a
2N/A file with any NDBM tool or with the following Perl
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
2N/A## txt2dbm -- convert txt map to dbm format
2N/A($txtmap, $dbmmap) = @ARGV;
2N/Aopen(TXT, "<$txtmap") or die "Couldn't open $txtmap!\n";
2N/Atie (%DB, 'NDBM_File', $dbmmap,O_RDWR|O_TRUNC|O_CREAT, 0644) or die "Couldn't create $dbmmap!\n";
2N/Awhile (<TXT>) {
2N/A next if (/^\s*#/ or /^\s*$/);
2N/A $DB{$1} = $2 if (/^\s*(\S+)\s+(\S+)/);
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A <
strong>Internal Function</
strong><
br>
2N/A MapType: <
code>int</
code>, MapSource: Internal Apache
2N/A <
p>Here the source is an internal Apache function.
2N/A Currently you cannot create your own, but the following
2N/A functions already exists:</
p>
2N/A <
li><
strong>toupper</
strong>:<
br>
2N/A Converts the looked up key to all upper case.</
li>
2N/A <
li><
strong>tolower</
strong>:<
br>
2N/A Converts the looked up key to all lower case.</
li>
2N/A <
li><
strong>escape</
strong>:<
br>
2N/A Translates special characters in the looked up key to
2N/A <
li><
strong>unescape</
strong>:<
br>
2N/A Translates hex-encodings in the looked up key back to
2N/A special characters.</
li>
2N/A <
strong>External Rewriting Program</
strong><
br>
2N/A MapType: <
code>prg</
code>, MapSource: Unix filesystem
2N/A path to valid regular file
2N/A <
p>Here the source is a program, not a map file. To
2N/A create it you can use the language of your choice, but
2N/A the result has to be a executable (<
em>
i.e.</
em>, either
2N/A object-code or a script with the magic cookie trick
2N/A <
p>This program is started once at startup of the Apache
2N/A servers and then communicates with the rewriting engine
2N/A over its <
code>stdin</
code> and <
code>stdout</
code>
2N/A file-handles. For each map-function lookup it will
2N/A receive the key to lookup as a newline-terminated string
2N/A on <
code>stdin</
code>. It then has to give back the
2N/A looked-up value as a newline-terminated string on
2N/A <
code>stdout</
code> or the four-character string
2N/A ``<
code>NULL</
code>'' if it fails (<
em>
i.e.</
em>, there
2N/A is no corresponding value for the given key). A trivial
2N/A program which will implement a 1:1 map (<
em>
i.e.</
em>,
2N/A key == value) could be:</
p>
2N/A<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
2N/Awhile (<STDIN>) {
2N/A #
...put here any transformations or lookups...
2N/A</
code></
td></
tr></
table></
blockquote>
2N/A <
p>But be very careful:</
p>
2N/A <
li>``<
em>Keep it simple, stupid</
em>'' (KISS), because
2N/A if this program hangs it will hang the Apache server
2N/A when the rule occurs.</
li>
2N/A <
li>Avoid one common mistake: never do buffered I/O on
2N/A <
code>stdout</
code>! This will cause a deadloop! Hence
2N/A the ``<
code>$|=1</
code>'' in the above example...</
li>
2N/A <
li>Use the <
a href="#rewritelock" class="directive"><
code class="directive">RewriteLock</
code></
a> directive to
2N/A define a lockfile mod_rewrite can use to synchronize the
2N/A communication to the program. By default no such
2N/A synchronization takes place.</
li>
2N/A The <
code class="directive">RewriteMap</
code> directive can occur more than
2N/A once. For each mapping-function use one
2N/A <
code class="directive">RewriteMap</
code> directive to declare its rewriting
2N/A mapfile. While you cannot <
strong>declare</
strong> a map in
2N/A per-directory context it is of course possible to
2N/A <
strong>use</
strong> this map in per-directory context.
2N/A<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"><
p align="center"><
strong>Note</
strong></
p> For plain text and DBM format files the
2N/Alooked-up keys are cached in-core until the <
code>mtime</
code> of the
2N/Amapfile changes or the server does a restart. This way you can have
2N/Amap-functions in rules which are used for <
strong>every</
strong>
2N/Arequest. This is no problem, because the external lookup only happens
2N/A</
td></
tr></
table></
blockquote>
2N/A</
usage><
hr/><
h2><
a name="RewriteOptions">RewriteOptions</
a> <
a name="rewriteoptions">Directive</
a></
h2><
table cellpadding="1" cellspacing="0" border="0" bgcolor="#cccccc"><
tr><
td><
table bgcolor="#ffffff"><
tr><
td><
strong>Description: </
strong></
td><
td>Sets some special options for the rewrite engine</
td></
tr><
tr><
td><
a href="directive-dict.html#Syntax" class="help">Syntax:</
a></
td><
td><
syntax>RewriteOptions <
em>Options</
em></
syntax></
td></
tr><
tr><
td><
a href="directive-dict.html#Default" class="help">Default:</
a></
td><
td><
code>None</
code></
td></
tr><
tr><
td><
a href="directive-dict.html#Context" class="help">Context:</
a></
td><
td>server config, virtual host, directory, .htaccess</
td></
tr><
tr><
td><
a href="directive-dict.html#Status" class="help">Status:</
a></
td><
td>Extension</
td></
tr><
tr><
td><
a href="directive-dict.html#Module" class="help">Module:</
a></
td><
td>mod_rewrite</
td></
tr></
table></
td></
tr></
table><
usage>
2N/A <
p>The <
code class="directive">RewriteOptions</
code> directive sets some
2N/A special options for the current per-server or per-directory
2N/A configuration. The <
em>Option</
em> strings can be one of the
2N/A <
li>'<
strong><
code>inherit</
code></
strong>'<
br>
2N/A This forces the current configuration to inherit the
2N/A configuration of the parent. In per-virtual-server context
2N/A this means that the maps, conditions and rules of the main
2N/A server are inherited. In per-directory context this means
2N/A that conditions and rules of the parent directory's
2N/A <
code>.htaccess</
code> configuration are inherited.</
li>
2N/A</
usage><
hr/><
h2><
a name="RewriteRule">RewriteRule</
a> <
a name="rewriterule">Directive</
a></
h2><
table cellpadding="1" cellspacing="0" border="0" bgcolor="#cccccc"><
tr><
td><
table bgcolor="#ffffff"><
tr><
td><
strong>Description: </
strong></
td><
td>Defines rules for the rewriting engine</
td></
tr><
tr><
td><
a href="directive-dict.html#Syntax" class="help">Syntax:</
a></
td><
td><
syntax>RewriteRule
2N/A <
em>Pattern</
em> <
em>Substitution</
em></
syntax></
td></
tr><
tr><
td><
a href="directive-dict.html#Default" class="help">Default:</
a></
td><
td><
code>None</
code></
td></
tr><
tr><
td><
a href="directive-dict.html#Context" class="help">Context:</
a></
td><
td>server config, virtual host, directory, .htaccess</
td></
tr><
tr><
td><
a href="directive-dict.html#Override" class="help">Override:</
a></
td><
td>FileInfo</
td></
tr><
tr><
td><
a href="directive-dict.html#Status" class="help">Status:</
a></
td><
td>Extension</
td></
tr><
tr><
td><
a href="directive-dict.html#Module" class="help">Module:</
a></
td><
td>mod_rewrite</
td></
tr></
table></
td></
tr></
table><
usage>
2N/A <
p>The <
code class="directive">RewriteRule</
code> directive is the real
2N/A rewriting workhorse. The directive can occur more than once.
2N/A Each directive then defines one single rewriting rule. The
2N/A <
strong>definition order</
strong> of these rules is
2N/A <
strong>important</
strong>, because this order is used when
2N/A applying the rules at run-time.</
p>
2N/A <
p><
a id="patterns" name="patterns"><
em>Pattern</
em></
a> can
2N/A later a POSIX) <
a id="regexp" name="regexp">regular
2N/A expression</
a> which gets applied to the current URL. Here
2N/A ``current'' means the value of the URL when this rule gets
2N/A applied. This may not be the originally requested URL,
2N/A because any number of rules may already have matched and made
2N/A alterations to it.</
p>
2N/A <
p>Some hints about the syntax of regular expressions:</
p>
2N/A <
table bgcolor="#F0F0F0" cellspacing="0" cellpadding="5">
2N/A<
strong>Text:</
strong>
2N/A <
strong><
code>.</
code></
strong> Any single character
2N/A <
strong><
code>[</
code></
strong>chars<
strong><
code>]</
code></
strong> Character class: One of chars
2N/A <
strong><
code>[^</
code></
strong>chars<
strong><
code>]</
code></
strong> Character class: None of chars
2N/A text1<
strong><
code>|</
code></
strong>text2 Alternative: text1 or text2
2N/A<
strong>Quantifiers:</
strong>
2N/A <
strong><
code>?</
code></
strong> 0 or 1 of the preceding text
2N/A <
strong><
code>*</
code></
strong> 0 or N of the preceding text (N > 0)
2N/A <
strong><
code>+</
code></
strong> 1 or N of the preceding text (N > 1)
2N/A<
strong>Grouping:</
strong>
2N/A <
strong><
code>(</
code></
strong>text<
strong><
code>)</
code></
strong> Grouping of text
2N/A (either to set the borders of an alternative or
2N/A for making backreferences where the <
strong>N</
strong>th group can
2N/A be used on the RHS of a RewriteRule with <
code>$</
code><
strong>N</
strong>)
2N/A<
strong>Anchors:</
strong>
2N/A <
strong><
code>^</
code></
strong> Start of line anchor
2N/A <
strong><
code>$</
code></
strong> End of line anchor
2N/A<
strong>Escaping:</
strong>
2N/A <
strong><
code>\</
code></
strong>char escape that particular char
2N/A (for instance to specify the chars "<
code>.[]()</
code>" <
em>etc.</
em>)
2N/A <
p>For more information about regular expressions either have
2N/A a look at your local regex(3) manpage or its
2N/A distribution. If you are interested in more detailed
2N/A information about regular expressions and their variants
2N/A (POSIX regex, Perl regex, <
em>etc.</
em>) have a look at the
2N/A following dedicated book on this topic:</
p>
2N/A <
em>Mastering Regular Expressions</
em><
br>
2N/A Nutshell Handbook Series<
br>
2N/A O'Reilly & Associates, Inc. 1997<
br>
2N/A ISBN 1-56592-257-3<
br>
2N/A <
p>Additionally in mod_rewrite the NOT character
2N/A ('<
code>!</
code>') is a possible pattern prefix. This gives
2N/A you the ability to negate a pattern; to say, for instance:
2N/A ``<
em>if the current URL does <
strong>NOT</
strong> match this
2N/A pattern</
em>''. This can be used for exceptional cases, where
2N/A it is easier to match the negative pattern, or as a last
2N/A<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"><
p align="center"><
strong>Notice</
strong></
p>
2N/AWhen using the NOT character
2N/A to negate a pattern you cannot have grouped wildcard
2N/A parts in the pattern. This is impossible because when the
2N/A pattern does NOT match, there are no contents for the
2N/A groups. In consequence, if negated patterns are used, you
2N/A cannot use <
code>$N</
code> in the substitution
2N/A</
td></
tr></
table></
blockquote>
2N/A <
p><
a id="rhs" name="rhs"><
em>Substitution</
em></
a> of a
2N/A rewriting rule is the string which is substituted for (or
2N/A replaces) the original URL for which <
em>Pattern</
em>
2N/A matched. Beside plain text you can use</
p>
2N/A <
li>back-references <
code>$N</
code> to the RewriteRule
2N/A <
li>back-references <
code>%N</
code> to the last matched
2N/A RewriteCond pattern</
li>
2N/A <
li>server-variables as in rule condition test-strings
2N/A (<
code>%{VARNAME}</
code>)</
li>
2N/A <
li><
a href="#mapfunc">mapping-function</
a> calls
2N/A (<
code>${mapname:key|default}</
code>)</
li>
2N/A Back-references are <
code>$</
code><
strong>N</
strong>
2N/A (<
strong>N</
strong>=0..9) identifiers which will be replaced
2N/A by the contents of the <
strong>N</
strong>th group of the
2N/A matched <
em>Pattern</
em>. The server-variables are the same
2N/A as for the <
em>TestString</
em> of a <
code>RewriteCond</
code>
2N/A directive. The mapping-functions come from the
2N/A <
code>RewriteMap</
code> directive and are explained there.
2N/A These three types of variables are expanded in the order of
2N/A <
p>As already mentioned above, all the rewriting rules are
2N/A applied to the <
em>Substitution</
em> (in the order of
2N/A definition in the config file). The URL is <
strong>completely
2N/A replaced</
strong> by the <
em>Substitution</
em> and the
2N/A rewriting process goes on until there are no more rules
2N/A unless explicitly terminated by a
2N/A <
code><
strong>L</
strong></
code> flag - see below.</
p>
2N/A <
p>There is a special substitution string named
2N/A '<
code>-</
code>' which means: <
strong>NO
2N/A substitution</
strong>! Sounds silly? No, it is useful to
2N/A provide rewriting rules which <
strong>only</
strong> match
2N/A some URLs but do no substitution, <
em>
e.g.</
em>, in
2N/A conjunction with the <
strong>C</
strong> (chain) flag to be
2N/A able to have more than one pattern to be applied before a
2N/A substitution occurs.</
p>
2N/A <
p>One more note: You can even create URLs in the
2N/A substitution string containing a query string part. Just use
2N/A a question mark inside the substitution string to indicate
2N/A that the following stuff should be re-injected into the
2N/A QUERY_STRING. When you want to erase an existing query
2N/A string, end the substitution string with just the question
2N/A<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"><
p align="center"><
strong>Note</
strong></
p>
2N/AThere is a special feature:
2N/A When you prefix a substitution field with
2N/A <
code>http://</
code><
em>thishost</
em>[<
em>:thisport</
em>]
2N/A then <
strong>mod_rewrite</
strong> automatically strips it
2N/A out. This auto-reduction on implicit external redirect
2N/A URLs is a useful and important feature when used in
2N/A combination with a mapping-function which generates the
2N/A hostname part. Have a look at the first example in the
2N/A example section below to understand this.
2N/A</
td></
tr></
table></
blockquote>
2N/A<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"><
p align="center"><
strong>Remember</
strong></
p>
2N/A An unconditional external
2N/A redirect to your own server will not work with the prefix
2N/A achieve such a self-redirect, you have to use the
2N/A <
strong>R</
strong>-flag (see below).
2N/A</
td></
tr></
table></
blockquote>
2N/A <
p>Additionally you can set special flags for
2N/A <
em>Substitution</
em> by appending</
p>
2N/A <
strong><
code>[</
code><
em>flags</
em><
code>]</
code></
strong>
as the third argument to the <
code>RewriteRule</
code>
directive. <
em>Flags</
em> is a comma-separated list of the
'<
strong><
code>redirect|R</
code>
[=<
em>code</
em>]</
strong>' (force <
a id="redirect" name="redirect"><
strong>r</
strong>edirect</
a>)<
br>
Prefix <
em>Substitution</
em> with
new URL a URI) to force a external redirection. If no
<
em>code</
em> is given a HTTP response of 302 (MOVED
TEMPORARILY) is used. If you want to use other response
codes in the range 300-400 just specify them as a number
or use one of the following symbolic names:
<
code>temp</
code> (default), <
code>permanent</
code>,
<
code>seeother</
code>. Use it for rules which should
canonicalize the URL and give it back to the client,
<
em>
e.g.</
em>, translate ``<
code>/~</
code>'' into
``<
code>/u/</
code>'' or always append a slash to
<
code>/u/</
code><
em>user</
em>, etc.<
br>
<
p><
strong>Note:</
strong> When you use this flag, make
sure that the substitution field is a valid URL! If not,
you are redirecting to an invalid location! And remember
that this flag itself only prefixes the URL with
continues. Usually you also want to stop and do the
redirection immediately. To stop the rewriting you also
have to provide the 'L' flag.</
p>
<
li>'<
strong><
code>forbidden|F</
code></
strong>' (force URL
to be <
strong>f</
strong>orbidden)<
br>
This forces the current URL to be forbidden,
<
em>
i.e.</
em>, it immediately sends back a HTTP response of
403 (FORBIDDEN). Use this flag in conjunction with
appropriate RewriteConds to conditionally block some
<
li>'<
strong><
code>gone|G</
code></
strong>' (force URL to be
<
strong>g</
strong>one)<
br>
This forces the current URL to be gone, <
em>
i.e.</
em>, it
immediately sends back a HTTP response of 410 (GONE). Use
this flag to mark pages which no longer exist as gone.</
li>
'<
strong><
code>proxy|P</
code></
strong>' (force
<
strong>p</
strong>roxy)<
br>
This flag forces the substitution part to be internally
forced as a proxy request and immediately (<
em>
i.e.</
em>,
rewriting rule processing stops here) put through the <
a href="mod_proxy.html">proxy module</
a>. You have to make
sure that the substitution string is a valid URI
(<
em>
e.g.</
em>, typically starting with
<
code>http://</
code><
em>hostname</
em>) which can be
handled by the Apache proxy module. If not you get an
error from the proxy module. Use this flag to achieve a
more powerful implementation of the <
a href="mod_proxy.html#proxypass">ProxyPass</
a> directive,
to map some remote stuff into the namespace of the local
<
p>Notice: To use this functionality make sure you have
the proxy module compiled into your Apache server
program. If you don't know please check whether
-l</
code>'' output. If yes, this functionality is
available to mod_rewrite. If not, then you first have to
rebuild the ``<
code>httpd</
code>'' program with mod_proxy
<
li>'<
strong><
code>last|L</
code></
strong>'
(<
strong>l</
strong>ast rule)<
br>
Stop the rewriting process here and don't apply any more
rewriting rules. This corresponds to the Perl
<
code>last</
code> command or the <
code>break</
code> command
from the C language. Use this flag to prevent the currently
rewritten URL from being rewritten further by following
rules. For example, use it to rewrite the root-path URL
('<
code>/</
code>') to a real one, <
em>
e.g.</
em>,
'<
code>/
e/
www/</
code>'.</
li>
<
li>'<
strong><
code>next|N</
code></
strong>'
(<
strong>n</
strong>ext round)<
br>
Re-run the rewriting process (starting again with the
first rewriting rule). Here the URL to match is again not
the original URL but the URL from the last rewriting rule.
This corresponds to the Perl <
code>next</
code> command or
the <
code>continue</
code> command from the C language. Use
this flag to restart the rewriting process, <
em>
i.e.</
em>,
to immediately go to the top of the loop.<
br>
<
strong>But be careful not to create an infinite
<
li>'<
strong><
code>chain|C</
code></
strong>'
(<
strong>c</
strong>hained with next rule)<
br>
This flag chains the current rule with the next rule
(which itself can be chained with the following rule,
<
em>etc.</
em>). This has the following effect: if a rule
matches, then processing continues as usual, <
em>
i.e.</
em>,
the flag has no effect. If the rule does
<
strong>not</
strong> match, then all following chained
rules are skipped. For instance, use it to remove the
``<
code>.www</
code>'' part inside a per-directory rule set
when you let an external redirect happen (where the
``<
code>.www</
code>'' part should not to occur!).</
li>
'<
strong><
code>type|T</
code></
strong>=<
em>MIME-type</
em>'
(force MIME <
strong>t</
strong>ype)<
br>
Force the MIME-type of the target file to be
<
em>MIME-type</
em>. For instance, this can be used to
simulate the <
code>mod_alias</
code> directive
<
code>ScriptAlias</
code> which internally forces all files
inside the mapped directory to have a MIME type of
'<
strong><
code>nosubreq|NS</
code></
strong>' (used only if
<
strong>n</
strong>o internal
<
strong>s</
strong>ub-request)<
br>
This flag forces the rewriting engine to skip a
rewriting rule if the current request is an internal
sub-request. For instance, sub-requests occur internally
in Apache when <
code>mod_include</
code> tries to find out
information about possible directory default files
(<
code>
index.xxx</
code>). On sub-requests it is not
always useful and even sometimes causes a failure to if
the complete set of rules are applied. Use this flag to
<
p>Use the following rule for your decision: whenever you
prefix some URLs with CGI-scripts to force them to be
processed by the CGI-script, the chance is high that you
will run into problems (or even overhead) on
sub-requests. In these cases, use this flag.</
p>
<
li>'<
strong><
code>nocase|NC</
code></
strong>'
(<
strong>n</
strong>o <
strong>c</
strong>ase)<
br>
This makes the <
em>Pattern</
em> case-insensitive,
<
em>
i.e.</
em>, there is no difference between 'A-Z' and
'a-z' when <
em>Pattern</
em> is matched against the current
<
li>'<
strong><
code>qsappend|QSA</
code></
strong>'
(<
strong>q</
strong>uery <
strong>s</
strong>tring
<
strong>a</
strong>ppend)<
br>
This flag forces the rewriting engine to append a query
string part in the substitution string to the existing one
instead of replacing it. Use this when you want to add more
data to the query string via a rewrite rule.</
li>
'<
strong><
code>noescape|NE</
code></
strong>'
(<
strong>n</
strong>o URI <
strong>e</
strong>scaping of
This flag keeps mod_rewrite from applying the usual URI
escaping rules to the result of a rewrite. Ordinarily,
special characters (such as '%', '$', ';', and so on)
will be escaped into their hexcode equivalents ('%25',
'%24', and '%3B', respectively); this flag prevents this
from being done. This allows percent symbols to appear in
<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE]
</
code></
td></
tr></
table></
blockquote>
which would turn '<
code>/
foo/
zed</
code>' into a safe
request for '<
code>/bar?arg=P1=zed</
code>'.
'<
strong><
code>passthrough|PT</
code></
strong>'
(<
strong>p</
strong>ass <
strong>t</
strong>hrough to next
This flag forces the rewriting engine to set the
<
code>uri</
code> field of the internal
<
code>request_rec</
code> structure to the value of the
<
code>filename</
code> field. This flag is just a hack to
be able to post-process the output of
<
code>RewriteRule</
code> directives by
<
code>Alias</
code>, <
code>ScriptAlias</
code>,
<
code>Redirect</
code>, <
em>etc.</
em> directives from
other URI-to-filename translators. A trivial example to
show the semantics: If you want to rewrite
<
code>/abc</
code> to <
code>/def</
code> via the rewriting
engine of <
code>mod_rewrite</
code> and then
<
code>/def</
code> to <
code>/ghi</
code> with
<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
RewriteRule ^/abc(.*) /def$1 [PT]<
br>
</
code></
td></
tr></
table></
blockquote>
If you omit the <
code>PT</
code> flag then
<
code>mod_rewrite</
code> will do its job fine,
<
em>
i.e.</
em>, it rewrites <
code>uri=/abc/...</
code> to
<
code>filename=/def/...</
code> as a full API-compliant
URI-to-filename translator should do. Then
<
code>mod_alias</
code> comes and tries to do a
URI-to-filename transition which will not work.
<
p>Note: <
strong>You have to use this flag if you want to
intermix directives of different modules which contain
URL-to-filename translators</
strong>. The typical example
is the use of <
code>mod_alias</
code> and
<
code>mod_rewrite</
code>..</
p>
<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"><
p align="center"><
strong>For Apache hackers</
strong></
p>
If the current Apache API had a filename-to-filename
hook additionally to the URI-to-filename hook then we
wouldn't need this flag! But without such a hook this
flag is the only solution. The Apache Group has
discussed this problem and will add such a hook in
</
td></
tr></
table></
blockquote>
<
li>'<
strong><
code>skip|S</
code></
strong>=<
em>num</
em>'
(<
strong>s</
strong>kip next rule(s))<
br>
This flag forces the rewriting engine to skip the next
<
em>num</
em> rules in sequence when the current rule
matches. Use this to make pseudo if-then-else constructs:
The last rule of the then-clause becomes
<
code>skip=N</
code> where N is the number of rules in the
else-clause. (This is <
strong>not</
strong> the same as the
'<
strong><
code>env|E=</
code></
strong><
em>VAR</
em>:<
em>VAL</
em>'
(set <
strong>e</
strong>nvironment variable)<
br>
This forces an environment variable named <
em>VAR</
em> to
be set to the value <
em>VAL</
em>, where <
em>VAL</
em> can
contain regexp backreferences <
code>$N</
code> and
<
code>%N</
code> which will be expanded. You can use this
flag more than once to set more than one variable. The
variables can be later dereferenced in many situations, but
usually from within XSSI (via <
code><!--#echo
var="VAR"--></
code>) or CGI (<
em>
e.g.</
em>
<
code>$ENV{'VAR'}</
code>). Additionally you can dereference
it in a following RewriteCond pattern via
<
code>%{ENV:VAR}</
code>. Use this to strip but remember
information from URLs.</
li>
<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"><
p align="center"><
strong>Note</
strong></
p> Never forget that <
em>Pattern</
em> is
applied to a complete URL in per-server configuration
files. <
strong>But in per-directory configuration files, the
per-directory prefix (which always is the same for a specific
directory!) is automatically <
em>removed</
em> for the pattern matching
and automatically <
em>added</
em> after the substitution has been
done.</
strong> This feature is essential for many sorts of rewriting,
because without this prefix stripping you have to match the parent
directory which is not always possible.
<
p>There is one exception: If a substitution string
starts with ``<
code>http://</
code>'' then the directory
prefix will <
strong>not</
strong> be added and an
external redirect or proxy throughput (if flag
<
strong>P</
strong> is used!) is forced!</
p>
</
td></
tr></
table></
blockquote>
<
blockquote><
table><
tr><
td bgcolor="#e0e5f5"><
p align="center"><
strong>Note</
strong></
p>
To enable the rewriting engine
for per-directory configuration files you need to set
``<
code>RewriteEngine On</
code>'' in these files
<
strong>and</
strong> ``<
code>Options
FollowSymLinks</
code>'' must be enabled. If your
administrator has disabled override of
<
code>FollowSymLinks</
code> for a user's directory, then
you cannot use the rewriting engine. This restriction is
needed for security reasons.
</
td></
tr></
table></
blockquote>
<
p>Here are all possible substitution combinations and their
<
p><
strong>Inside per-server configuration
<
table bgcolor="#F0F0F0" cellspacing="0" cellpadding="5">
<
strong>Given Rule</
strong> <
strong>Resulting Substitution</
strong>
---------------------------------------------- ----------------------------------
^/somepath(.*) otherpath$1 not supported, because invalid!
^/somepath(.*) otherpath$1 [R] not supported, because invalid!
^/somepath(.*) otherpath$1 [P] not supported, because invalid!
---------------------------------------------- ----------------------------------
^/somepath(.*) /otherpath$1 [P] not supported, because silly!
---------------------------------------------- ----------------------------------
---------------------------------------------- ----------------------------------
(the [R] flag is redundant)
<
p><
strong>Inside per-directory configuration for
<
code>/somepath</
code><
br>
(<
em>
i.e.</
em>, file <
code>.htaccess</
code> in dir
<
code>RewriteBase /somepath</
code>)<
br>
<
table bgcolor="#F0F0F0" cellspacing="0" cellpadding="5">
<
strong>Given Rule</
strong> <
strong>Resulting Substitution</
strong>
---------------------------------------------- ----------------------------------
^localpath(.*) otherpath$1 [P] not supported, because silly!
---------------------------------------------- ----------------------------------
^localpath(.*) /otherpath$1 [P] not supported, because silly!
---------------------------------------------- ----------------------------------
---------------------------------------------- ----------------------------------
(the [R] flag is redundant)
<
p><
strong>Example:</
strong></
p>
<
p>We want to rewrite URLs of the form </
p>
<
code>/</
code> <
em>Language</
em> <
code>/~</
code>
<
em>Realname</
em> <
code>/.../</
code> <
em>File</
em>
<
code>/u/</
code> <
em>Username</
em> <
code>/.../</
code>
<
em>File</
em> <
code>.</
code> <
em>Language</
em>
<
p>We take the rewrite mapfile from above and save it under
add the following lines to the Apache server configuration
<
blockquote><
table cellpadding="10"><
tr><
td bgcolor="#eeeeee"><
code>
RewriteRule ^/([^/]+)/~([^/]+)/(.*)$ /u/${real-to-user:$2|nobody}/$3.$1
</
code></
td></
tr></
table></
blockquote>
</
usage><
hr/><
h3 align="center">Apache HTTP Server Version 2.0</
h3><
a href="./"><
img alt="Index" src="/images/index.gif"/></
a><
a href="../"><
img alt="Home" src="/images/home.gif"/></
a></
blockquote></
body></
html>