1 <html xmlns="http://www.w3.org/TR/xhtml1/strict"><head><!--
2 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
3 This file is generated from xml source: DO NOT EDIT
4 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5 --><title>mod_rewrite- Apache HTTP Server</title><link href="../style/manual.css" type="text/css" rel="stylesheet"/></head><body><blockquote><div align="center"><img src="../images/sub.gif" alt="[APACHE DOCUMENTATION]"/><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 nowrap="nowrap" valign="top"><span class="help">Description:</span></td><td>Provides a rule-based rewriting engine to rewrite requested
6 URLs on the fly</td></tr><tr><td nowrap="nowrap"><a href="module-dict.html#Status" class="help">Status:</a></td><td>Extension</td></tr><tr><td nowrap="nowrap"><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>
8 <em>``The great thing about mod_rewrite is it gives you
9 all the configurability and flexibility of Sendmail.
10 The downside to mod_rewrite is that it gives you all
11 the configurability and flexibility of Sendmail.''</em><br>
13 -- Brian Behlendorf<br>
14 Apache Group
19 <em>`` Despite the tons of examples and docs,
20 mod_rewrite is voodoo. Damned cool voodoo, but still
23 -- Brian Moore<br>
24 bem@news.cmc.net
29 <p>Welcome to mod_rewrite, the Swiss Army Knife of URL
32 <p>This module uses a rule-based rewriting engine (based on a
33 regular-expression parser) to rewrite requested URLs on the
34 fly. It supports an unlimited number of rules and an
35 unlimited number of attached rule conditions for each rule to
36 provide a really flexible and powerful URL manipulation
37 mechanism. The URL manipulations can depend on various tests,
38 for instance server variables, environment variables, HTTP
39 headers, time stamps and even external database lookups in
40 various formats can be used to achieve a really granular URL
43 <p>This module operates on the full URLs (including the
44 path-info part) both in per-server context
45 (<code>httpd.conf</code>) and per-directory context
46 (<code>.htaccess</code>) and can even generate query-string
47 parts on result. The rewritten result can lead to internal
48 sub-processing, external request redirection or even to an
49 internal proxy throughput.</p>
51 <p>But all this functionality and flexibility has its
52 drawback: complexity. So don't expect to understand this
53 entire module in just one day.</p>
55 <p>This module was invented and originally written in April
56 1996 and gifted exclusively to the The Apache Group in July 1997
60 <a href="http://www.engelschall.com/"><code>Ralf S.
61 Engelschall</code></a><br>
62 <a href="mailto:rse@engelschall.com"><code>rse@engelschall.com</code></a><br>
63 <a href="http://www.engelschall.com/"><code>www.engelschall.com</code></a>
65 <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>
67 <p>The internal processing of this module is very complex but
68 needs to be explained once even to the average user to avoid
69 common mistakes and to let you exploit its full
72 <h3><a name="InternalAPI">API Phases</a></h3>
74 <p>First you have to understand that when Apache processes a
75 HTTP request it does this in phases. A hook for each of these
76 phases is provided by the Apache API. Mod_rewrite uses two of
77 these hooks: the URL-to-filename translation hook which is
78 used after the HTTP request has been read but before any
79 authorization starts and the Fixup hook which is triggered
80 after the authorization phases and after the per-directory
81 config files (<code>.htaccess</code>) have been read, but
82 before the content handler is activated.</p>
84 <p>So, after a request comes in and Apache has determined the
85 corresponding server (or virtual server) the rewriting engine
86 starts processing of all mod_rewrite directives from the
87 per-server configuration in the URL-to-filename phase. A few
88 steps later when the final data directories are found, the
89 per-directory configuration directives of mod_rewrite are
90 triggered in the Fixup phase. In both situations mod_rewrite
91 rewrites URLs either to new URLs or to filenames, although
92 there is no obvious distinction between them. This is a usage
93 of the API which was not intended to be this way when the API
94 was designed, but as of Apache 1.x this is the only way
95 mod_rewrite can operate. To make this point more clear
96 remember the following two points:</p>
99 <li>Although mod_rewrite rewrites URLs to URLs, URLs to
100 filenames and even filenames to filenames, the API
101 currently provides only a URL-to-filename hook. In Apache
102 2.0 the two missing hooks will be added to make the
103 processing more clear. But this point has no drawbacks for
104 the user, it is just a fact which should be remembered:
105 Apache does more in the URL-to-filename hook than the API
109 Unbelievably mod_rewrite provides URL manipulations in
110 per-directory context, <em>i.e.</em>, within
111 <code>.htaccess</code> files, although these are reached
112 a very long time after the URLs have been translated to
113 filenames. It has to be this way because
114 <code>.htaccess</code> files live in the filesystem, so
115 processing has already reached this stage. In other
116 words: According to the API phases at this time it is too
117 late for any URL manipulations. To overcome this chicken
118 and egg problem mod_rewrite uses a trick: When you
119 manipulate a URL/filename in per-directory context
120 mod_rewrite first rewrites the filename back to its
121 corresponding URL (which is usually impossible, but see
122 the <code>RewriteBase</code> directive below for the
123 trick to achieve this) and then initiates a new internal
124 sub-request with the new URL. This restarts processing of
127 <p>Again mod_rewrite tries hard to make this complicated
128 step totally transparent to the user, but you should
129 remember here: While URL manipulations in per-server
130 context are really fast and efficient, per-directory
131 rewrites are slow and inefficient due to this chicken and
132 egg problem. But on the other hand this is the only way
133 mod_rewrite can provide (locally restricted) URL
134 manipulations to the average user.</p>
138 <p>Don't forget these two points!</p>
141 <h3><a name="InternalRuleset">Ruleset Processing</a></h3>
143 <p>Now when mod_rewrite is triggered in these two API phases, it
144 reads the configured rulesets from its configuration
145 structure (which itself was either created on startup for
146 per-server context or during the directory walk of the Apache
147 kernel for per-directory context). Then the URL rewriting
148 engine is started with the contained ruleset (one or more
149 rules together with their conditions). The operation of the
150 URL rewriting engine itself is exactly the same for both
151 configuration contexts. Only the final result processing is
154 <p>The order of rules in the ruleset is important because the
155 rewriting engine processes them in a special (and not very
156 obvious) order. The rule is this: The rewriting engine loops
157 through the ruleset rule by rule (<a href="#rewriterule" class="directive"><code class="directive">RewriteRule</code></a> directives) and
158 when a particular rule matches it optionally loops through
159 existing corresponding conditions (<code>RewriteCond</code>
160 directives). For historical reasons the conditions are given
161 first, and so the control flow is a little bit long-winded. See
162 Figure 1 for more details.</p>
164 <img src="../images/mod_rewrite_fig1.gif" border="1" width="428" height="385" alt="[Needs graphics capability to display]">
165 <p><strong>Figure 1:</strong>The control flow through the rewriting ruleset</p>
167 <p>As you can see, first the URL is matched against the
168 <em>Pattern</em> of each rule. When it fails mod_rewrite
169 immediately stops processing this rule and continues with the
170 next rule. If the <em>Pattern</em> matches, mod_rewrite looks
171 for corresponding rule conditions. If none are present, it
172 just substitutes the URL with a new value which is
173 constructed from the string <em>Substitution</em> and goes on
174 with its rule-looping. But if conditions exist, it starts an
175 inner loop for processing them in the order that they are
176 listed. For conditions the logic is different: we don't match
177 a pattern against the current URL. Instead we first create a
178 string <em>TestString</em> by expanding variables,
179 back-references, map lookups, <em>etc.</em> and then we try
180 to match <em>CondPattern</em> against it. If the pattern
181 doesn't match, the complete set of conditions and the
182 corresponding rule fails. If the pattern matches, then the
183 next condition is processed until no more conditions are
184 available. If all conditions match, processing is continued
185 with the substitution of the URL with
186 <em>Substitution</em>.</p>
190 <h3><a name="quoting">Quoting Special Characters</a></h3>
192 <p>As of Apache 1.3.20, special characters in
193 <em>TestString</em> and <em>Substitution</em> strings can be
194 escaped (that is, treated as normal characters without their
195 usual special meaning) by prefixing them with a slosh ('\')
196 character. In other words, you can include an actual
197 dollar-sign character in a <em>Substitution</em> string by
198 using '<code>\$</code>'; this keeps mod_rewrite from trying
199 to treat it as a backreference.</p>
202 <h3><a name="InternalBackRefs">Regex Back-Reference Availability</a></h3>
204 <p>One important thing here has to be remembered: Whenever you
205 use parentheses in <em>Pattern</em> or in one of the
206 <em>CondPattern</em>, back-references are internally created
207 which can be used with the strings <code>$N</code> and
208 <code>%N</code> (see below). These are available for creating
209 the strings <em>Substitution</em> and <em>TestString</em>.
210 Figure 2 shows to which locations the back-references are
211 transfered for expansion.</p>
214 <img src="../images/mod_rewrite_fig2.gif" border="1" width="381" height="179" alt="[Needs graphics capability to display]">
215 <p><strong>Figure 2:</strong> The back-reference flow through a rule.</p>
217 <p>We know this was a crash course on mod_rewrite's internal
218 processing. But you will benefit from this knowledge when
219 reading the following documentation of the available
223 <h2><a name="EnvVar">Environment Variables</a></h2>
225 <p>This module keeps track of two additional (non-standard)
226 CGI/SSI environment variables named <code>SCRIPT_URL</code>
227 and <code>SCRIPT_URI</code>. These contain the
228 <em>logical</em> Web-view to the current resource, while the
229 standard CGI/SSI variables <code>SCRIPT_NAME</code> and
230 <code>SCRIPT_FILENAME</code> contain the <em>physical</em>
233 <p>Notice: These variables hold the URI/URL <em>as they were
234 initially requested</em>, <em>i.e.</em>, <em>before</em> any
235 rewriting. This is important because the rewriting process is
236 primarily used to rewrite logical URLs to physical
239 <p><strong>Example:</strong></p>
241 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
243 SCRIPT_NAME=/sw/lib/w3s/tree/global/u/rse/.www/index.html
244 SCRIPT_FILENAME=/u/rse/.www/index.html
246 SCRIPT_URI=http://en1.engelschall.com/u/rse/
248 </code></td></tr></table></blockquote>
250 <h2><a name="Solutions">Practical Solutions</a></h2>
252 <p>We also have an <a href="../misc/rewriteguide.html">URL
253 Rewriting Guide</a> available, which provides a collection of
254 practical solutions for URL-based problems. There you can
255 find real-life rulesets and additional information about
257 <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 nowrap="nowrap"><strong>Description: </strong></td><td>Sets the base URL for per-directory rewrites</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Syntax" class="help">Syntax:</a></td><td>RewriteBase <em>URL-path</em></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Default" class="help">Default:</a></td><td><code>See usage for information.</code></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Context" class="help">Context:</a></td><td>directory, .htaccess</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Override" class="help">Override:</a></td><td>FileInfo</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Status" class="help">Status:</a></td><td>Extension</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Module" class="help">Module:</a></td><td>mod_rewrite</td></tr></table></td></tr></table>
258 <p>The <code class="directive">RewriteBase</code> directive explicitly
259 sets the base URL for per-directory rewrites. As you will see
260 below, <a href="#rewriterule" class="directive"><code class="directive">RewriteRule</code></a>
261 can be used in per-directory config files
262 (<code>.htaccess</code>). There it will act locally,
263 <em>i.e.</em>, the local directory prefix is stripped at this
264 stage of processing and your rewriting rules act only on the
265 remainder. At the end it is automatically added back to the
266 path. The default setting is; <code class="directive">RewriteBase</code> <em>physical-directory-path</em></p>
268 <p>When a substitution occurs for a new URL, this module has
269 to re-inject the URL into the server processing. To be able
270 to do this it needs to know what the corresponding URL-prefix
271 or URL-base is. By default this prefix is the corresponding
272 filepath itself. <strong>But at most websites URLs are NOT
273 directly related to physical filename paths, so this
274 assumption will usually be wrong!</strong> There you have to
275 use the <code>RewriteBase</code> directive to specify the
276 correct URL-prefix.</p>
278 <blockquote><table><tr><td bgcolor="#e0e5f5"> If your webserver's URLs are <strong>not</strong> directly
279 related to physical file paths, you have to use
280 <code class="directive">RewriteBase</code> in every <code>.htaccess</code>
281 files where you want to use <a href="#rewriterule" class="directive"><code class="directive">RewriteRule</code></a> directives.
282 </td></tr></table></blockquote>
284 <p> For example, assume the following per-directory config file:</p>
286 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
289 # /abc/def/.htaccess -- per-dir config file for directory /abc/def
290 # Remember: /abc/def is the physical path of /xyz, <em>i.e.</em>, the server
291 # has a 'Alias /xyz /abc/def' directive <em>e.g.</em>
296 # let the server know that we were reached via /xyz and not
297 # via the physical path prefix /abc/def
300 # now the rewriting rules
301 RewriteRule ^oldstuff\.html$ newstuff.html
303 </code></td></tr></table></blockquote>
305 <p>In the above example, a request to
306 <code>/xyz/oldstuff.html</code> gets correctly rewritten to
307 the physical file <code>/abc/def/newstuff.html</code>.</p>
309 <blockquote><table><tr><td bgcolor="#e0e5f5"><p align="center"><strong>For Apache Hackers</strong></p>
310 <p>The following list gives detailed information about
311 the internal processing steps:</p>
317 /xyz/oldstuff.html -> /abc/def/oldstuff.html (per-server Alias)
318 /abc/def/oldstuff.html -> /abc/def/newstuff.html (per-dir RewriteRule)
319 /abc/def/newstuff.html -> /xyz/newstuff.html (per-dir RewriteBase)
320 /xyz/newstuff.html -> /abc/def/newstuff.html (per-server Alias)
323 /abc/def/newstuff.html
325 <p>This seems very complicated but is
326 the correct Apache internal processing, because the
327 per-directory rewriting comes too late in the
328 process. So, when it occurs the (rewritten) request
329 has to be re-injected into the Apache kernel! BUT:
330 While this seems like a serious overhead, it really
331 isn't, because this re-injection happens fully
332 internally to the Apache server and the same
333 procedure is used by many other operations inside
334 Apache. So, you can be sure the design and
335 implementation is correct.</p>
336 </td></tr></table></blockquote>
338 <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 nowrap="nowrap"><strong>Description: </strong></td><td>Defines a condition under which rewriting will take place
339 </td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Syntax" class="help">Syntax:</a></td><td> RewriteCond
340 <em>TestString</em> <em>CondPattern</em></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Default" class="help">Default:</a></td><td><code>None</code></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Context" class="help">Context:</a></td><td>server config, virtual host, directory, .htaccess</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Override" class="help">Override:</a></td><td>FileInfo</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Status" class="help">Status:</a></td><td>Extension</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Module" class="help">Module:</a></td><td>mod_rewrite</td></tr></table></td></tr></table>
341 <p>The <code class="directive">RewriteCond</code> directive defines a
342 rule condition. Precede a <a href="#rewriterule" class="directive"><code class="directive">RewriteRule</code></a> directive with one
343 or more <code class="directive">RewriteCond</code> directives. The following
344 rewriting rule is only used if its pattern matches the current
345 state of the URI <strong>and</strong> if these additional
346 conditions apply too.</p>
348 <p><em>TestString</em> is a string which can contains the
349 following expanded constructs in addition to plain text:</p>
353 <strong>RewriteRule backreferences</strong>: These are
354 backreferences of the form
357 <strong><code>$N</code></strong>
359 (0 <= N <= 9) which provide access to the grouped
360 parts (parenthesis!) of the pattern from the
361 corresponding <code>RewriteRule</code> directive (the one
362 following the current bunch of <code>RewriteCond</code>
367 <strong>RewriteCond backreferences</strong>: These are
368 backreferences of the form
371 <strong><code>%N</code></strong>
373 (1 <= N <= 9) which provide access to the grouped
374 parts (parentheses!) of the pattern from the last matched
375 <code>RewriteCond</code> directive in the current bunch
380 <strong>RewriteMap expansions</strong>: These are
381 expansions of the form
384 <strong><code>${mapname:key|default}</code></strong>
386 See <a href="#mapfunc">the documentation for
387 RewriteMap</a> for more details.
391 <strong>Server-Variables</strong>: These are variables of
395 <strong><code>%{</code> <em>NAME_OF_VARIABLE</em>
396 <code>}</code></strong>
398 where <em>NAME_OF_VARIABLE</em> can be a string taken
399 from the following list:
401 <table bgcolor="#F0F0F0" cellspacing="0" cellpadding="5">
404 <strong>HTTP headers:</strong>
406 <p>HTTP_USER_AGENT<br>
411 HTTP_PROXY_CONNECTION<br>
417 <strong>connection & request:</strong>
434 <strong>server internals:</strong>
447 <strong>system stuff:</strong>
461 <strong>specials:</strong>
473 <blockquote><table><tr><td bgcolor="#e0e5f5">
474 <p>These variables all
475 correspond to the similarly named HTTP
476 MIME-headers, C variables of the Apache server or
477 <code>struct tm</code> fields of the Unix system.
478 Most are documented elsewhere in the Manual or in
479 the CGI specification. Those that are special to
480 mod_rewrite include:</p>
483 <dt><code>IS_SUBREQ</code></dt>
485 <dd>Will contain the text "true" if the request
486 currently being processed is a sub-request,
487 "false" otherwise. Sub-requests may be generated
488 by modules that need to resolve additional files
489 or URIs in order to complete their tasks.</dd>
491 <dt><code>API_VERSION</code></dt>
493 <dd>This is the version of the Apache module API
494 (the internal interface between server and
495 module) in the current httpd build, as defined in
496 include/ap_mmn.h. The module API version
497 corresponds to the version of Apache in use (in
498 the release version of Apache 1.3.14, for
499 instance, it is 19990320:10), but is mainly of
500 interest to module authors.</dd>
502 <dt><code>THE_REQUEST</code></dt>
504 <dd>The full HTTP request line sent by the
505 browser to the server (e.g., "<code>GET
506 /index.html HTTP/1.1</code>"). This does not
507 include any additional headers sent by the
510 <dt><code>REQUEST_URI</code></dt>
512 <dd>The resource requested in the HTTP request
513 line. (In the example above, this would be
516 <dt><code>REQUEST_FILENAME</code></dt>
518 <dd>The full local filesystem path to the file or
519 script matching the request.</dd>
521 </td></tr></table></blockquote>
525 <p>Special Notes:</p>
528 <li>The variables SCRIPT_FILENAME and REQUEST_FILENAME
529 contain the same value, <em>i.e.</em>, the value of the
530 <code>filename</code> field of the internal
531 <code>request_rec</code> structure of the Apache server.
532 The first name is just the commonly known CGI variable name
533 while the second is the consistent counterpart to
534 REQUEST_URI (which contains the value of the
535 <code>uri</code> field of <code>request_rec</code>).</li>
537 <li>There is the special format:
538 <code>%{ENV:variable}</code> where <em>variable</em> can be
539 any environment variable. This is looked-up via internal
540 Apache structures and (if not found there) via
541 <code>getenv()</code> from the Apache server process.</li>
543 <li>There is the special format:
544 <code>%{HTTP:header}</code> where <em>header</em> can be
545 any HTTP MIME-header name. This is looked-up from the HTTP
546 request. Example: <code>%{HTTP:Proxy-Connection}</code> is
547 the value of the HTTP header
548 ``<code>Proxy-Connection:</code>''.</li>
550 <li>There is the special format
551 <code>%{LA-U:variable}</code> for look-aheads which perform
552 an internal (URL-based) sub-request to determine the final
553 value of <em>variable</em>. Use this when you want to use a
554 variable for rewriting which is actually set later in an
555 API phase and thus is not available at the current stage.
556 For instance when you want to rewrite according to the
557 <code>REMOTE_USER</code> variable from within the
558 per-server context (<code>httpd.conf</code> file) you have
559 to use <code>%{LA-U:REMOTE_USER}</code> because this
560 variable is set by the authorization phases which come
561 <em>after</em> the URL translation phase where mod_rewrite
562 operates. On the other hand, because mod_rewrite implements
563 its per-directory context (<code>.htaccess</code> file) via
564 the Fixup phase of the API and because the authorization
565 phases come <em>before</em> this phase, you just can use
566 <code>%{REMOTE_USER}</code> there.</li>
568 <li>There is the special format:
569 <code>%{LA-F:variable}</code> which performs an internal
570 (filename-based) sub-request to determine the final value
571 of <em>variable</em>. Most of the time this is the same as
575 <p><em>CondPattern</em> is the condition pattern,
576 <em>i.e.</em>, a regular expression which is applied to the
577 current instance of the <em>TestString</em>, <em>i.e.</em>,
578 <em>TestString</em> is evaluated and then matched against
579 <em>CondPattern</em>.</p>
581 <p><strong>Remember:</strong> <em>CondPattern</em> is a
582 standard <em>Extended Regular Expression</em> with some
586 <li>You can prefix the pattern string with a
587 '<code>!</code>' character (exclamation mark) to specify a
588 <strong>non</strong>-matching pattern.</li>
591 There are some special variants of <em>CondPatterns</em>.
592 Instead of real regular expression strings you can also
593 use one of the following:
596 <li>'<strong><CondPattern</strong>' (is lexically
598 Treats the <em>CondPattern</em> as a plain string and
599 compares it lexically to <em>TestString</em>. True if
600 <em>TestString</em> is lexically lower than
601 <em>CondPattern</em>.</li>
603 <li>'<strong>>CondPattern</strong>' (is lexically
605 Treats the <em>CondPattern</em> as a plain string and
606 compares it lexically to <em>TestString</em>. True if
607 <em>TestString</em> is lexically greater than
608 <em>CondPattern</em>.</li>
610 <li>'<strong>=CondPattern</strong>' (is lexically
612 Treats the <em>CondPattern</em> as a plain string and
613 compares it lexically to <em>TestString</em>. True if
614 <em>TestString</em> is lexically equal to
615 <em>CondPattern</em>, i.e the two strings are exactly
616 equal (character by character). If <em>CondPattern</em>
617 is just <code>""</code> (two quotation marks) this
618 compares <em>TestString</em> to the empty string.</li>
620 <li>'<strong>-d</strong>' (is
621 <strong>d</strong>irectory)<br>
622 Treats the <em>TestString</em> as a pathname and tests
623 if it exists and is a directory.</li>
625 <li>'<strong>-f</strong>' (is regular
626 <strong>f</strong>ile)<br>
627 Treats the <em>TestString</em> as a pathname and tests
628 if it exists and is a regular file.</li>
630 <li>'<strong>-s</strong>' (is regular file with
631 <strong>s</strong>ize)<br>
632 Treats the <em>TestString</em> as a pathname and tests
633 if it exists and is a regular file with size greater
636 <li>'<strong>-l</strong>' (is symbolic
637 <strong>l</strong>ink)<br>
638 Treats the <em>TestString</em> as a pathname and tests
639 if it exists and is a symbolic link.</li>
641 <li>'<strong>-F</strong>' (is existing file via
643 Checks if <em>TestString</em> is a valid file and
644 accessible via all the server's currently-configured
645 access controls for that path. This uses an internal
646 subrequest to determine the check, so use it with care
647 because it decreases your servers performance!</li>
649 <li>'<strong>-U</strong>' (is existing URL via
651 Checks if <em>TestString</em> is a valid URL and
652 accessible via all the server's currently-configured
653 access controls for that path. This uses an internal
654 subrequest to determine the check, so use it with care
655 because it decreases your server's performance!</li>
658 <blockquote><table><tr><td bgcolor="#e0e5f5"><p align="center"><strong>Notice</strong></p>
659 All of these tests can
660 also be prefixed by an exclamation mark ('!') to
661 negate their meaning.
662 </td></tr></table></blockquote>
666 <p>Additionally you can set special flags for
667 <em>CondPattern</em> by appending</p>
670 <strong><code>[</code><em>flags</em><code>]</code></strong>
673 <p>as the third argument to the <code>RewriteCond</code>
674 directive. <em>Flags</em> is a comma-separated list of the
678 <li>'<strong><code>nocase|NC</code></strong>'
679 (<strong>n</strong>o <strong>c</strong>ase)<br>
680 This makes the test case-insensitive, <em>i.e.</em>, there
681 is no difference between 'A-Z' and 'a-z' both in the
682 expanded <em>TestString</em> and the <em>CondPattern</em>.
683 This flag is effective only for comparisons between
684 <em>TestString</em> and <em>CondPattern</em>. It has no
685 effect on filesystem and subrequest checks.</li>
688 '<strong><code>ornext|OR</code></strong>'
689 (<strong>or</strong> next condition)<br>
690 Use this to combine rule conditions with a local OR
691 instead of the implicit AND. Typical example:
693 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
695 RewriteCond %{REMOTE_HOST} ^host1.* [OR]
696 RewriteCond %{REMOTE_HOST} ^host2.* [OR]
697 RewriteCond %{REMOTE_HOST} ^host3.*
698 RewriteRule ...some special stuff for any of these hosts...
700 </code></td></tr></table></blockquote>
702 Without this flag you would have to write the cond/rule
707 <p><strong>Example:</strong></p>
709 <p>To rewrite the Homepage of a site according to the
710 ``<code>User-Agent:</code>'' header of the request, you can
711 use the following: </p>
713 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
715 RewriteCond %{HTTP_USER_AGENT} ^Mozilla.*
716 RewriteRule ^/$ /homepage.max.html [L]
718 RewriteCond %{HTTP_USER_AGENT} ^Lynx.*
719 RewriteRule ^/$ /homepage.min.html [L]
721 RewriteRule ^/$ /homepage.std.html [L]
723 </code></td></tr></table></blockquote>
725 <p>Interpretation: If you use Netscape Navigator as your
726 browser (which identifies itself as 'Mozilla'), then you
727 get the max homepage, which includes Frames, <em>etc.</em>
728 If you use the Lynx browser (which is Terminal-based), then
729 you get the min homepage, which contains no images, no
730 tables, <em>etc.</em> If you use any other browser you get
731 the standard homepage.</p>
733 <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 nowrap="nowrap"><strong>Description: </strong></td><td>Enables or disables runtime rewriting engine</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Syntax" class="help">Syntax:</a></td><td>RewriteEngine on|off</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Default" class="help">Default:</a></td><td><code>RewriteEngine off</code></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Context" class="help">Context:</a></td><td>server config, virtual host, directory, .htaccess</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Override" class="help">Override:</a></td><td>FileInfo</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Status" class="help">Status:</a></td><td>Extension</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Module" class="help">Module:</a></td><td>mod_rewrite</td></tr></table></td></tr></table>
735 <p>The <code class="directive">RewriteEngine</code> directive enables or
736 disables the runtime rewriting engine. If it is set to
737 <code>off</code> this module does no runtime processing at
738 all. It does not even update the <code>SCRIPT_URx</code>
739 environment variables.</p>
741 <p>Use this directive to disable the module instead of
742 commenting out all the <a href="#rewriterule" class="directive"><code class="directive">RewriteRule</code></a> directives!</p>
744 <p>Note that, by default, rewrite configurations are not
745 inherited. This means that you need to have a
746 <code>RewriteEngine on</code> directive for each virtual host
747 in which you wish to use it.</p>
748 <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 nowrap="nowrap"><strong>Description: </strong></td><td>Sets the name of the lock file used for RewriteMap
749 synchronization</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Syntax" class="help">Syntax:</a></td><td>RewriteLock <em>file-path</em></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Default" class="help">Default:</a></td><td><code>None</code></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Context" class="help">Context:</a></td><td>server config</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Status" class="help">Status:</a></td><td>Extension</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Module" class="help">Module:</a></td><td>mod_rewrite</td></tr></table></td></tr></table>
750 <p>This directive sets the filename for a synchronization
751 lockfile which mod_rewrite needs to communicate with <a href="#rewritemap" class="directive"><code class="directive">RewriteMap</code></a>
752 <em>programs</em>. Set this lockfile to a local path (not on a
753 NFS-mounted device) when you want to use a rewriting
754 map-program. It is not required for other types of rewriting
756 <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 nowrap="nowrap"><strong>Description: </strong></td><td>Sets the name of the file used for logging rewrite engine
757 processing</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Syntax" class="help">Syntax:</a></td><td>RewriteLog <em>file-path</em></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Context" class="help">Context:</a></td><td>server config, virtual host</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Status" class="help">Status:</a></td><td>Extension</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Module" class="help">Module:</a></td><td>mod_rewrite</td></tr></table></td></tr></table>
758 <p>The <code class="directive">RewriteLog</code> directive sets the name
759 of the file to which the server logs any rewriting actions it
760 performs. If the name does not begin with a slash
761 ('<code>/</code>') then it is assumed to be relative to the
762 <em>Server Root</em>. The directive should occur only once per
765 <blockquote><table><tr><td bgcolor="#e0e5f5"> To disable the logging of
766 rewriting actions it is not recommended to set
767 <em>Filename</em> to <code>/dev/null</code>, because
768 although the rewriting engine does not then output to a
769 logfile it still creates the logfile output internally.
770 <strong>This will slow down the server with no advantage
771 to the administrator!</strong> To disable logging either
772 remove or comment out the <code class="directive">RewriteLog</code>
773 directive or use <code>RewriteLogLevel 0</code>!
774 </td></tr></table></blockquote>
776 <blockquote><table><tr><td bgcolor="#e0e5f5"><p align="center"><strong>Security</strong></p>
778 See the <a href="../misc/security_tips.html">Apache Security Tips</a>
779 document for details on why your security could be compromised if the
780 directory where logfiles are stored is writable by anyone other than
781 the user that starts the server.
782 </td></tr></table></blockquote>
784 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><p align="center"><strong>Example</strong></p><code>
785 RewriteLog "/usr/local/var/apache/logs/rewrite.log"
786 </code></td></tr></table></blockquote>
788 <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 nowrap="nowrap"><strong>Description: </strong></td><td>Sets the verbosity of the log file used by the rewrite
789 engine</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Syntax" class="help">Syntax:</a></td><td>RewriteLogLevel <em>Level</em></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Default" class="help">Default:</a></td><td><code>RerwiteLogLevel 0</code></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Context" class="help">Context:</a></td><td>server config, virtual host</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Status" class="help">Status:</a></td><td>Extension</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Module" class="help">Module:</a></td><td>mod_rewrite</td></tr></table></td></tr></table>
790 <p>The <code class="directive">RewriteLogLevel</code> directive sets the
791 verbosity level of the rewriting logfile. The default level 0
792 means no logging, while 9 or more means that practically all
793 actions are logged.</p>
795 <p>To disable the logging of rewriting actions simply set
796 <em>Level</em> to 0. This disables all rewrite action
799 <blockquote><table><tr><td bgcolor="#e0e5f5"> Using a high value for
800 <em>Level</em> will slow down your Apache server
801 dramatically! Use the rewriting logfile at a
802 <em>Level</em> greater than 2 only for debugging!
803 </td></tr></table></blockquote>
805 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><p align="center"><strong>Example</strong></p><code>
807 </code></td></tr></table></blockquote>
809 <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 nowrap="nowrap"><strong>Description: </strong></td><td>Defines a mapping function for key-lookup</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Syntax" class="help">Syntax:</a></td><td>RewriteMap <em>MapName</em> <em>MapType</em>:<em>MapSource</em>
810 </td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Default" class="help">Default:</a></td><td><code>None</code></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Context" class="help">Context:</a></td><td>server config, virtual host</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Status" class="help">Status:</a></td><td>Extension</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Module" class="help">Module:</a></td><td>mod_rewrite</td></tr></table></td></tr></table>
811 <p>The <code class="directive">RewriteMap</code> directive defines a
812 <em>Rewriting Map</em> which can be used inside rule
813 substitution strings by the mapping-functions to
814 insert/substitute fields through a key lookup. The source of
815 this lookup can be of various types.</p>
817 <p>The <a id="mapfunc" name="mapfunc"><em>MapName</em></a> is
818 the name of the map and will be used to specify a
819 mapping-function for the substitution strings of a rewriting
820 rule via one of the following constructs:</p>
823 <strong><code>${</code> <em>MapName</em> <code>:</code>
824 <em>LookupKey</em> <code>}</code><br>
825 <code>${</code> <em>MapName</em> <code>:</code>
826 <em>LookupKey</em> <code>|</code> <em>DefaultValue</em>
827 <code>}</code></strong>
830 <p>When such a construct occurs the map <em>MapName</em> is
831 consulted and the key <em>LookupKey</em> is looked-up. If the
832 key is found, the map-function construct is substituted by
833 <em>SubstValue</em>. If the key is not found then it is
834 substituted by <em>DefaultValue</em> or by the empty string
835 if no <em>DefaultValue</em> was specified.</p>
837 <p>The following combinations for <em>MapType</em> and
838 <em>MapSource</em> can be used:</p>
842 <strong>Standard Plain Text</strong><br>
843 MapType: <code>txt</code>, MapSource: Unix filesystem
844 path to valid regular file
846 <p>This is the standard rewriting map feature where the
847 <em>MapSource</em> is a plain ASCII file containing
848 either blank lines, comment lines (starting with a '#'
849 character) or pairs like the following - one per
853 <strong><em>MatchingKey</em>
854 <em>SubstValue</em></strong>
857 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><p align="center"><strong>Example</strong></p><code>
860 ## map.txt -- rewriting map
863 Ralf.S.Engelschall rse # Bastard Operator From Hell
864 Mr.Joe.Average joe # Mr. Average
866 </code></td></tr></table></blockquote>
868 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
869 RewriteMap real-to-user txt:/path/to/file/map.txt
870 </code></td></tr></table></blockquote>
874 <strong>Randomized Plain Text</strong><br>
875 MapType: <code>rnd</code>, MapSource: Unix filesystem
876 path to valid regular file
878 <p>This is identical to the Standard Plain Text variant
879 above but with a special post-processing feature: After
880 looking up a value it is parsed according to contained
881 ``<code>|</code>'' characters which have the meaning of
882 ``or''. In other words they indicate a set of
883 alternatives from which the actual returned value is
884 chosen randomly. Although this sounds crazy and useless,
885 it was actually designed for load balancing in a reverse
886 proxy situation where the looked up values are server
889 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
892 ## map.txt -- rewriting map
895 static www1|www2|www3|www4
898 </code></td></tr></table></blockquote>
900 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
901 RewriteMap servers rnd:/path/to/file/map.txt
902 </code></td></tr></table></blockquote>
906 <strong>Hash File</strong><br>
907 MapType: <code>dbm</code>, MapSource: Unix filesystem
908 path to valid regular file
910 <p>Here the source is a binary NDBM format file
911 containing the same contents as a <em>Plain Text</em>
912 format file, but in a special representation which is
913 optimized for really fast lookups. You can create such a
914 file with any NDBM tool or with the following Perl
917 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
921 ## txt2dbm -- convert txt map to dbm format
927 ($txtmap, $dbmmap) = @ARGV;
929 open(TXT, "<$txtmap") or die "Couldn't open $txtmap!\n";
930 tie (%DB, 'NDBM_File', $dbmmap,O_RDWR|O_TRUNC|O_CREAT, 0644) or die "Couldn't create $dbmmap!\n";
932 while (<TXT>) {
933 next if (/^\s*#/ or /^\s*$/);
934 $DB{$1} = $2 if (/^\s*(\S+)\s+(\S+)/);
940 </code></td></tr></table></blockquote>
942 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
943 $ txt2dbm map.txt map.db
944 </code></td></tr></table></blockquote>
948 <strong>Internal Function</strong><br>
949 MapType: <code>int</code>, MapSource: Internal Apache
952 <p>Here the source is an internal Apache function.
953 Currently you cannot create your own, but the following
954 functions already exists:</p>
957 <li><strong>toupper</strong>:<br>
958 Converts the looked up key to all upper case.</li>
960 <li><strong>tolower</strong>:<br>
961 Converts the looked up key to all lower case.</li>
963 <li><strong>escape</strong>:<br>
964 Translates special characters in the looked up key to
967 <li><strong>unescape</strong>:<br>
968 Translates hex-encodings in the looked up key back to
969 special characters.</li>
974 <strong>External Rewriting Program</strong><br>
975 MapType: <code>prg</code>, MapSource: Unix filesystem
976 path to valid regular file
978 <p>Here the source is a program, not a map file. To
979 create it you can use the language of your choice, but
980 the result has to be a executable (<em>i.e.</em>, either
981 object-code or a script with the magic cookie trick
982 '<code>#!/path/to/interpreter</code>' as the first
985 <p>This program is started once at startup of the Apache
986 servers and then communicates with the rewriting engine
987 over its <code>stdin</code> and <code>stdout</code>
988 file-handles. For each map-function lookup it will
989 receive the key to lookup as a newline-terminated string
990 on <code>stdin</code>. It then has to give back the
991 looked-up value as a newline-terminated string on
992 <code>stdout</code> or the four-character string
993 ``<code>NULL</code>'' if it fails (<em>i.e.</em>, there
994 is no corresponding value for the given key). A trivial
995 program which will implement a 1:1 map (<em>i.e.</em>,
996 key == value) could be:</p>
998 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
1002 while (<STDIN>) {
1003 # ...put here any transformations or lookups...
1007 </code></td></tr></table></blockquote>
1009 <p>But be very careful:</p>
1012 <li>``<em>Keep it simple, stupid</em>'' (KISS), because
1013 if this program hangs it will hang the Apache server
1014 when the rule occurs.</li>
1016 <li>Avoid one common mistake: never do buffered I/O on
1017 <code>stdout</code>! This will cause a deadloop! Hence
1018 the ``<code>$|=1</code>'' in the above example...</li>
1020 <li>Use the <a href="#rewritelock" class="directive"><code class="directive">RewriteLock</code></a> directive to
1021 define a lockfile mod_rewrite can use to synchronize the
1022 communication to the program. By default no such
1023 synchronization takes place.</li>
1027 <p>The <code class="directive">RewriteMap</code> directive can occur more than
1028 once. For each mapping-function use one
1029 <code class="directive">RewriteMap</code> directive to declare its rewriting
1030 mapfile. While you cannot <strong>declare</strong> a map in
1031 per-directory context it is of course possible to
1032 <strong>use</strong> this map in per-directory context. </p>
1034 <blockquote><table><tr><td bgcolor="#e0e5f5"><p align="center"><strong>Note</strong></p> For plain text and DBM format files the
1035 looked-up keys are cached in-core until the <code>mtime</code> of the
1036 mapfile changes or the server does a restart. This way you can have
1037 map-functions in rules which are used for <strong>every</strong>
1038 request. This is no problem, because the external lookup only happens
1040 </td></tr></table></blockquote>
1042 <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 nowrap="nowrap"><strong>Description: </strong></td><td>Sets some special options for the rewrite engine</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Syntax" class="help">Syntax:</a></td><td>RewriteOptions <em>Options</em></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Default" class="help">Default:</a></td><td><code>None</code></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Context" class="help">Context:</a></td><td>server config, virtual host, directory, .htaccess</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Status" class="help">Status:</a></td><td>Extension</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Module" class="help">Module:</a></td><td>mod_rewrite</td></tr></table></td></tr></table>
1044 <p>The <code class="directive">RewriteOptions</code> directive sets some
1045 special options for the current per-server or per-directory
1046 configuration. The <em>Option</em> strings can be one of the
1050 <li>'<strong><code>inherit</code></strong>'<br>
1051 This forces the current configuration to inherit the
1052 configuration of the parent. In per-virtual-server context
1053 this means that the maps, conditions and rules of the main
1054 server are inherited. In per-directory context this means
1055 that conditions and rules of the parent directory's
1056 <code>.htaccess</code> configuration are inherited.</li>
1058 <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 nowrap="nowrap"><strong>Description: </strong></td><td>Defines rules for the rewriting engine</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Syntax" class="help">Syntax:</a></td><td>RewriteRule
1059 <em>Pattern</em> <em>Substitution</em></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Default" class="help">Default:</a></td><td><code>None</code></td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Context" class="help">Context:</a></td><td>server config, virtual host, directory, .htaccess</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Override" class="help">Override:</a></td><td>FileInfo</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Status" class="help">Status:</a></td><td>Extension</td></tr><tr><td nowrap="nowrap"><a href="directive-dict.html#Module" class="help">Module:</a></td><td>mod_rewrite</td></tr></table></td></tr></table>
1060 <p>The <code class="directive">RewriteRule</code> directive is the real
1061 rewriting workhorse. The directive can occur more than once.
1062 Each directive then defines one single rewriting rule. The
1063 <strong>definition order</strong> of these rules is
1064 <strong>important</strong>, because this order is used when
1065 applying the rules at run-time.</p>
1067 <p><a id="patterns" name="patterns"><em>Pattern</em></a> can
1068 be (for Apache 1.1.x a System V8 and for Apache 1.2.x and
1069 later a POSIX) <a id="regexp" name="regexp">regular
1070 expression</a> which gets applied to the current URL. Here
1071 ``current'' means the value of the URL when this rule gets
1072 applied. This may not be the originally requested URL,
1073 because any number of rules may already have matched and made
1074 alterations to it.</p>
1076 <p>Some hints about the syntax of regular expressions:</p>
1078 <table bgcolor="#F0F0F0" cellspacing="0" cellpadding="5">
1082 <strong>Text:</strong>
1083 <strong><code>.</code></strong> Any single character
1084 <strong><code>[</code></strong>chars<strong><code>]</code></strong> Character class: One of chars
1085 <strong><code>[^</code></strong>chars<strong><code>]</code></strong> Character class: None of chars
1086 text1<strong><code>|</code></strong>text2 Alternative: text1 or text2
1088 <strong>Quantifiers:</strong>
1089 <strong><code>?</code></strong> 0 or 1 of the preceding text
1090 <strong><code>*</code></strong> 0 or N of the preceding text (N > 0)
1091 <strong><code>+</code></strong> 1 or N of the preceding text (N > 1)
1093 <strong>Grouping:</strong>
1094 <strong><code>(</code></strong>text<strong><code>)</code></strong> Grouping of text
1095 (either to set the borders of an alternative or
1096 for making backreferences where the <strong>N</strong>th group can
1097 be used on the RHS of a RewriteRule with <code>$</code><strong>N</strong>)
1099 <strong>Anchors:</strong>
1100 <strong><code>^</code></strong> Start of line anchor
1101 <strong><code>$</code></strong> End of line anchor
1103 <strong>Escaping:</strong>
1104 <strong><code>\</code></strong>char escape that particular char
1105 (for instance to specify the chars "<code>.[]()</code>" <em>etc.</em>)
1111 <p>For more information about regular expressions either have
1112 a look at your local regex(3) manpage or its
1113 <code>src/regex/regex.3</code> copy in the Apache 1.3
1114 distribution. If you are interested in more detailed
1115 information about regular expressions and their variants
1116 (POSIX regex, Perl regex, <em>etc.</em>) have a look at the
1117 following dedicated book on this topic:</p>
1120 <em>Mastering Regular Expressions</em><br>
1121 Jeffrey E.F. Friedl<br>
1122 Nutshell Handbook Series<br>
1123 O'Reilly & Associates, Inc. 1997<br>
1124 ISBN 1-56592-257-3<br>
1127 <p>Additionally in mod_rewrite the NOT character
1128 ('<code>!</code>') is a possible pattern prefix. This gives
1129 you the ability to negate a pattern; to say, for instance:
1130 ``<em>if the current URL does <strong>NOT</strong> match this
1131 pattern</em>''. This can be used for exceptional cases, where
1132 it is easier to match the negative pattern, or as a last
1135 <blockquote><table><tr><td bgcolor="#e0e5f5"><p align="center"><strong>Notice</strong></p>
1136 When using the NOT character
1137 to negate a pattern you cannot have grouped wildcard
1138 parts in the pattern. This is impossible because when the
1139 pattern does NOT match, there are no contents for the
1140 groups. In consequence, if negated patterns are used, you
1141 cannot use <code>$N</code> in the substitution
1143 </td></tr></table></blockquote>
1145 <p><a id="rhs" name="rhs"><em>Substitution</em></a> of a
1146 rewriting rule is the string which is substituted for (or
1147 replaces) the original URL for which <em>Pattern</em>
1148 matched. Beside plain text you can use</p>
1151 <li>back-references <code>$N</code> to the RewriteRule
1154 <li>back-references <code>%N</code> to the last matched
1155 RewriteCond pattern</li>
1157 <li>server-variables as in rule condition test-strings
1158 (<code>%{VARNAME}</code>)</li>
1160 <li><a href="#mapfunc">mapping-function</a> calls
1161 (<code>${mapname:key|default}</code>)</li>
1163 <p>Back-references are <code>$</code><strong>N</strong>
1164 (<strong>N</strong>=0..9) identifiers which will be replaced
1165 by the contents of the <strong>N</strong>th group of the
1166 matched <em>Pattern</em>. The server-variables are the same
1167 as for the <em>TestString</em> of a <code>RewriteCond</code>
1168 directive. The mapping-functions come from the
1169 <code>RewriteMap</code> directive and are explained there.
1170 These three types of variables are expanded in the order of
1171 the above list. </p>
1173 <p>As already mentioned above, all the rewriting rules are
1174 applied to the <em>Substitution</em> (in the order of
1175 definition in the config file). The URL is <strong>completely
1176 replaced</strong> by the <em>Substitution</em> and the
1177 rewriting process goes on until there are no more rules
1178 unless explicitly terminated by a
1179 <code><strong>L</strong></code> flag - see below.</p>
1181 <p>There is a special substitution string named
1182 '<code>-</code>' which means: <strong>NO
1183 substitution</strong>! Sounds silly? No, it is useful to
1184 provide rewriting rules which <strong>only</strong> match
1185 some URLs but do no substitution, <em>e.g.</em>, in
1186 conjunction with the <strong>C</strong> (chain) flag to be
1187 able to have more than one pattern to be applied before a
1188 substitution occurs.</p>
1190 <p>One more note: You can even create URLs in the
1191 substitution string containing a query string part. Just use
1192 a question mark inside the substitution string to indicate
1193 that the following stuff should be re-injected into the
1194 QUERY_STRING. When you want to erase an existing query
1195 string, end the substitution string with just the question
1198 <blockquote><table><tr><td bgcolor="#e0e5f5"><p align="center"><strong>Note</strong></p>
1199 There is a special feature:
1200 When you prefix a substitution field with
1201 <code>http://</code><em>thishost</em>[<em>:thisport</em>]
1202 then <strong>mod_rewrite</strong> automatically strips it
1203 out. This auto-reduction on implicit external redirect
1204 URLs is a useful and important feature when used in
1205 combination with a mapping-function which generates the
1206 hostname part. Have a look at the first example in the
1207 example section below to understand this.
1208 </td></tr></table></blockquote>
1210 <blockquote><table><tr><td bgcolor="#e0e5f5"><p align="center"><strong>Remember</strong></p>
1211 An unconditional external
1212 redirect to your own server will not work with the prefix
1213 <code>http://thishost</code> because of this feature. To
1214 achieve such a self-redirect, you have to use the
1215 <strong>R</strong>-flag (see below).
1216 </td></tr></table></blockquote>
1218 <p>Additionally you can set special flags for
1219 <em>Substitution</em> by appending</p>
1222 <strong><code>[</code><em>flags</em><code>]</code></strong>
1225 as the third argument to the <code>RewriteRule</code>
1226 directive. <em>Flags</em> is a comma-separated list of the
1227 following flags: </p>
1231 '<strong><code>redirect|R</code>
1232 [=<em>code</em>]</strong>' (force <a id="redirect" name="redirect"><strong>r</strong>edirect</a>)<br>
1233 Prefix <em>Substitution</em> with
1234 <code>http://thishost[:thisport]/</code> (which makes the
1235 new URL a URI) to force a external redirection. If no
1236 <em>code</em> is given a HTTP response of 302 (MOVED
1237 TEMPORARILY) is used. If you want to use other response
1238 codes in the range 300-400 just specify them as a number
1239 or use one of the following symbolic names:
1240 <code>temp</code> (default), <code>permanent</code>,
1241 <code>seeother</code>. Use it for rules which should
1242 canonicalize the URL and give it back to the client,
1243 <em>e.g.</em>, translate ``<code>/~</code>'' into
1244 ``<code>/u/</code>'' or always append a slash to
1245 <code>/u/</code><em>user</em>, etc.<br>
1248 <p><strong>Note:</strong> When you use this flag, make
1249 sure that the substitution field is a valid URL! If not,
1250 you are redirecting to an invalid location! And remember
1251 that this flag itself only prefixes the URL with
1252 <code>http://thishost[:thisport]/</code>, rewriting
1253 continues. Usually you also want to stop and do the
1254 redirection immediately. To stop the rewriting you also
1255 have to provide the 'L' flag.</p>
1258 <li>'<strong><code>forbidden|F</code></strong>' (force URL
1259 to be <strong>f</strong>orbidden)<br>
1260 This forces the current URL to be forbidden,
1261 <em>i.e.</em>, it immediately sends back a HTTP response of
1262 403 (FORBIDDEN). Use this flag in conjunction with
1263 appropriate RewriteConds to conditionally block some
1266 <li>'<strong><code>gone|G</code></strong>' (force URL to be
1267 <strong>g</strong>one)<br>
1268 This forces the current URL to be gone, <em>i.e.</em>, it
1269 immediately sends back a HTTP response of 410 (GONE). Use
1270 this flag to mark pages which no longer exist as gone.</li>
1273 '<strong><code>proxy|P</code></strong>' (force
1274 <strong>p</strong>roxy)<br>
1275 This flag forces the substitution part to be internally
1276 forced as a proxy request and immediately (<em>i.e.</em>,
1277 rewriting rule processing stops here) put through the <a href="mod_proxy.html">proxy module</a>. You have to make
1278 sure that the substitution string is a valid URI
1279 (<em>e.g.</em>, typically starting with
1280 <code>http://</code><em>hostname</em>) which can be
1281 handled by the Apache proxy module. If not you get an
1282 error from the proxy module. Use this flag to achieve a
1283 more powerful implementation of the <a href="mod_proxy.html#proxypass">ProxyPass</a> directive,
1284 to map some remote stuff into the namespace of the local
1287 <p>Notice: To use this functionality make sure you have
1288 the proxy module compiled into your Apache server
1289 program. If you don't know please check whether
1290 <code>mod_proxy.c</code> is part of the ``<code>httpd
1291 -l</code>'' output. If yes, this functionality is
1292 available to mod_rewrite. If not, then you first have to
1293 rebuild the ``<code>httpd</code>'' program with mod_proxy
1297 <li>'<strong><code>last|L</code></strong>'
1298 (<strong>l</strong>ast rule)<br>
1299 Stop the rewriting process here and don't apply any more
1300 rewriting rules. This corresponds to the Perl
1301 <code>last</code> command or the <code>break</code> command
1302 from the C language. Use this flag to prevent the currently
1303 rewritten URL from being rewritten further by following
1304 rules. For example, use it to rewrite the root-path URL
1305 ('<code>/</code>') to a real one, <em>e.g.</em>,
1306 '<code>/e/www/</code>'.</li>
1308 <li>'<strong><code>next|N</code></strong>'
1309 (<strong>n</strong>ext round)<br>
1310 Re-run the rewriting process (starting again with the
1311 first rewriting rule). Here the URL to match is again not
1312 the original URL but the URL from the last rewriting rule.
1313 This corresponds to the Perl <code>next</code> command or
1314 the <code>continue</code> command from the C language. Use
1315 this flag to restart the rewriting process, <em>i.e.</em>,
1316 to immediately go to the top of the loop.<br>
1317 <strong>But be careful not to create an infinite
1320 <li>'<strong><code>chain|C</code></strong>'
1321 (<strong>c</strong>hained with next rule)<br>
1322 This flag chains the current rule with the next rule
1323 (which itself can be chained with the following rule,
1324 <em>etc.</em>). This has the following effect: if a rule
1325 matches, then processing continues as usual, <em>i.e.</em>,
1326 the flag has no effect. If the rule does
1327 <strong>not</strong> match, then all following chained
1328 rules are skipped. For instance, use it to remove the
1329 ``<code>.www</code>'' part inside a per-directory rule set
1330 when you let an external redirect happen (where the
1331 ``<code>.www</code>'' part should not to occur!).</li>
1334 '<strong><code>type|T</code></strong>=<em>MIME-type</em>'
1335 (force MIME <strong>t</strong>ype)<br>
1336 Force the MIME-type of the target file to be
1337 <em>MIME-type</em>. For instance, this can be used to
1338 simulate the <code>mod_alias</code> directive
1339 <code>ScriptAlias</code> which internally forces all files
1340 inside the mapped directory to have a MIME type of
1341 ``<code>application/x-httpd-cgi</code>''.</li>
1344 '<strong><code>nosubreq|NS</code></strong>' (used only if
1345 <strong>n</strong>o internal
1346 <strong>s</strong>ub-request)<br>
1347 This flag forces the rewriting engine to skip a
1348 rewriting rule if the current request is an internal
1349 sub-request. For instance, sub-requests occur internally
1350 in Apache when <code>mod_include</code> tries to find out
1351 information about possible directory default files
1352 (<code>index.xxx</code>). On sub-requests it is not
1353 always useful and even sometimes causes a failure to if
1354 the complete set of rules are applied. Use this flag to
1355 exclude some rules.<br>
1358 <p>Use the following rule for your decision: whenever you
1359 prefix some URLs with CGI-scripts to force them to be
1360 processed by the CGI-script, the chance is high that you
1361 will run into problems (or even overhead) on
1362 sub-requests. In these cases, use this flag.</p>
1365 <li>'<strong><code>nocase|NC</code></strong>'
1366 (<strong>n</strong>o <strong>c</strong>ase)<br>
1367 This makes the <em>Pattern</em> case-insensitive,
1368 <em>i.e.</em>, there is no difference between 'A-Z' and
1369 'a-z' when <em>Pattern</em> is matched against the current
1372 <li>'<strong><code>qsappend|QSA</code></strong>'
1373 (<strong>q</strong>uery <strong>s</strong>tring
1374 <strong>a</strong>ppend)<br>
1375 This flag forces the rewriting engine to append a query
1376 string part in the substitution string to the existing one
1377 instead of replacing it. Use this when you want to add more
1378 data to the query string via a rewrite rule.</li>
1381 '<strong><code>noescape|NE</code></strong>'
1382 (<strong>n</strong>o URI <strong>e</strong>scaping of
1384 This flag keeps mod_rewrite from applying the usual URI
1385 escaping rules to the result of a rewrite. Ordinarily,
1386 special characters (such as '%', '$', ';', and so on)
1387 will be escaped into their hexcode equivalents ('%25',
1388 '%24', and '%3B', respectively); this flag prevents this
1389 from being done. This allows percent symbols to appear in
1391 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
1392 RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE]
1393 </code></td></tr></table></blockquote>
1395 which would turn '<code>/foo/zed</code>' into a safe
1396 request for '<code>/bar?arg=P1=zed</code>'.
1400 '<strong><code>passthrough|PT</code></strong>'
1401 (<strong>p</strong>ass <strong>t</strong>hrough to next
1403 This flag forces the rewriting engine to set the
1404 <code>uri</code> field of the internal
1405 <code>request_rec</code> structure to the value of the
1406 <code>filename</code> field. This flag is just a hack to
1407 be able to post-process the output of
1408 <code>RewriteRule</code> directives by
1409 <code>Alias</code>, <code>ScriptAlias</code>,
1410 <code>Redirect</code>, <em>etc.</em> directives from
1411 other URI-to-filename translators. A trivial example to
1412 show the semantics: If you want to rewrite
1413 <code>/abc</code> to <code>/def</code> via the rewriting
1414 engine of <code>mod_rewrite</code> and then
1415 <code>/def</code> to <code>/ghi</code> with
1416 <code>mod_alias</code>:
1417 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
1418 RewriteRule ^/abc(.*) /def$1 [PT]<br>
1420 </code></td></tr></table></blockquote>
1421 If you omit the <code>PT</code> flag then
1422 <code>mod_rewrite</code> will do its job fine,
1423 <em>i.e.</em>, it rewrites <code>uri=/abc/...</code> to
1424 <code>filename=/def/...</code> as a full API-compliant
1425 URI-to-filename translator should do. Then
1426 <code>mod_alias</code> comes and tries to do a
1427 URI-to-filename transition which will not work.
1429 <p>Note: <strong>You have to use this flag if you want to
1430 intermix directives of different modules which contain
1431 URL-to-filename translators</strong>. The typical example
1432 is the use of <code>mod_alias</code> and
1433 <code>mod_rewrite</code>..</p>
1435 <blockquote><table><tr><td bgcolor="#e0e5f5"><p align="center"><strong>For Apache hackers</strong></p>
1436 If the current Apache API had a filename-to-filename
1437 hook additionally to the URI-to-filename hook then we
1438 wouldn't need this flag! But without such a hook this
1439 flag is the only solution. The Apache Group has
1440 discussed this problem and will add such a hook in
1442 </td></tr></table></blockquote>
1445 <li>'<strong><code>skip|S</code></strong>=<em>num</em>'
1446 (<strong>s</strong>kip next rule(s))<br>
1447 This flag forces the rewriting engine to skip the next
1448 <em>num</em> rules in sequence when the current rule
1449 matches. Use this to make pseudo if-then-else constructs:
1450 The last rule of the then-clause becomes
1451 <code>skip=N</code> where N is the number of rules in the
1452 else-clause. (This is <strong>not</strong> the same as the
1453 'chain|C' flag!)</li>
1456 '<strong><code>env|E=</code></strong><em>VAR</em>:<em>VAL</em>'
1457 (set <strong>e</strong>nvironment variable)<br>
1458 This forces an environment variable named <em>VAR</em> to
1459 be set to the value <em>VAL</em>, where <em>VAL</em> can
1460 contain regexp backreferences <code>$N</code> and
1461 <code>%N</code> which will be expanded. You can use this
1462 flag more than once to set more than one variable. The
1463 variables can be later dereferenced in many situations, but
1464 usually from within XSSI (via <code><!--#echo
1465 var="VAR"--></code>) or CGI (<em>e.g.</em>
1466 <code>$ENV{'VAR'}</code>). Additionally you can dereference
1467 it in a following RewriteCond pattern via
1468 <code>%{ENV:VAR}</code>. Use this to strip but remember
1469 information from URLs.</li>
1472 '<strong><code>cookie|CO=</code></strong><em>NAME</em>:<em>VAL</em>:<em>domain</em>[:<em>lifetime</em>]'
1473 (set <strong>co</strong>cookie)<br>
1474 This sets a cookie on the client's browser. The cookie's name
1475 is specified by <em>NAME</em> and the value is
1476 <em>VAL</em>. The <em>domain</em> field is the domain of the
1477 cookie, such as '.apache.org' and the optional <em>lifetime</em>
1478 is the lifetime of the cookie in minutes.</li>
1482 <blockquote><table><tr><td bgcolor="#e0e5f5"><p align="center"><strong>Note</strong></p> Never forget that <em>Pattern</em> is
1483 applied to a complete URL in per-server configuration
1484 files. <strong>But in per-directory configuration files, the
1485 per-directory prefix (which always is the same for a specific
1486 directory!) is automatically <em>removed</em> for the pattern matching
1487 and automatically <em>added</em> after the substitution has been
1488 done.</strong> This feature is essential for many sorts of rewriting,
1489 because without this prefix stripping you have to match the parent
1490 directory which is not always possible.
1492 <p>There is one exception: If a substitution string
1493 starts with ``<code>http://</code>'' then the directory
1494 prefix will <strong>not</strong> be added and an
1495 external redirect or proxy throughput (if flag
1496 <strong>P</strong> is used!) is forced!</p>
1497 </td></tr></table></blockquote>
1499 <blockquote><table><tr><td bgcolor="#e0e5f5"><p align="center"><strong>Note</strong></p>
1500 To enable the rewriting engine
1501 for per-directory configuration files you need to set
1502 ``<code>RewriteEngine On</code>'' in these files
1503 <strong>and</strong> ``<code>Options
1504 FollowSymLinks</code>'' must be enabled. If your
1505 administrator has disabled override of
1506 <code>FollowSymLinks</code> for a user's directory, then
1507 you cannot use the rewriting engine. This restriction is
1508 needed for security reasons.
1509 </td></tr></table></blockquote>
1511 <p>Here are all possible substitution combinations and their
1514 <p><strong>Inside per-server configuration
1515 (<code>httpd.conf</code>)<br>
1516 for request ``<code>GET
1517 /somepath/pathinfo</code>'':</strong><br>
1520 <table bgcolor="#F0F0F0" cellspacing="0" cellpadding="5">
1524 <strong>Given Rule</strong> <strong>Resulting Substitution</strong>
1525 ---------------------------------------------- ----------------------------------
1526 ^/somepath(.*) otherpath$1 not supported, because invalid!
1528 ^/somepath(.*) otherpath$1 [R] not supported, because invalid!
1530 ^/somepath(.*) otherpath$1 [P] not supported, because invalid!
1531 ---------------------------------------------- ----------------------------------
1532 ^/somepath(.*) /otherpath$1 /otherpath/pathinfo
1534 ^/somepath(.*) /otherpath$1 [R] http://thishost/otherpath/pathinfo
1535 via external redirection
1537 ^/somepath(.*) /otherpath$1 [P] not supported, because silly!
1538 ---------------------------------------------- ----------------------------------
1539 ^/somepath(.*) http://thishost/otherpath$1 /otherpath/pathinfo
1541 ^/somepath(.*) http://thishost/otherpath$1 [R] http://thishost/otherpath/pathinfo
1542 via external redirection
1544 ^/somepath(.*) http://thishost/otherpath$1 [P] not supported, because silly!
1545 ---------------------------------------------- ----------------------------------
1546 ^/somepath(.*) http://otherhost/otherpath$1 http://otherhost/otherpath/pathinfo
1547 via external redirection
1549 ^/somepath(.*) http://otherhost/otherpath$1 [R] http://otherhost/otherpath/pathinfo
1550 via external redirection
1551 (the [R] flag is redundant)
1553 ^/somepath(.*) http://otherhost/otherpath$1 [P] http://otherhost/otherpath/pathinfo
1560 <p><strong>Inside per-directory configuration for
1561 <code>/somepath</code><br>
1562 (<em>i.e.</em>, file <code>.htaccess</code> in dir
1563 <code>/physical/path/to/somepath</code> containing
1564 <code>RewriteBase /somepath</code>)<br>
1565 for request ``<code>GET
1566 /somepath/localpath/pathinfo</code>'':</strong><br>
1569 <table bgcolor="#F0F0F0" cellspacing="0" cellpadding="5">
1573 <strong>Given Rule</strong> <strong>Resulting Substitution</strong>
1574 ---------------------------------------------- ----------------------------------
1575 ^localpath(.*) otherpath$1 /somepath/otherpath/pathinfo
1577 ^localpath(.*) otherpath$1 [R] http://thishost/somepath/otherpath/pathinfo
1578 via external redirection
1580 ^localpath(.*) otherpath$1 [P] not supported, because silly!
1581 ---------------------------------------------- ----------------------------------
1582 ^localpath(.*) /otherpath$1 /otherpath/pathinfo
1584 ^localpath(.*) /otherpath$1 [R] http://thishost/otherpath/pathinfo
1585 via external redirection
1587 ^localpath(.*) /otherpath$1 [P] not supported, because silly!
1588 ---------------------------------------------- ----------------------------------
1589 ^localpath(.*) http://thishost/otherpath$1 /otherpath/pathinfo
1591 ^localpath(.*) http://thishost/otherpath$1 [R] http://thishost/otherpath/pathinfo
1592 via external redirection
1594 ^localpath(.*) http://thishost/otherpath$1 [P] not supported, because silly!
1595 ---------------------------------------------- ----------------------------------
1596 ^localpath(.*) http://otherhost/otherpath$1 http://otherhost/otherpath/pathinfo
1597 via external redirection
1599 ^localpath(.*) http://otherhost/otherpath$1 [R] http://otherhost/otherpath/pathinfo
1600 via external redirection
1601 (the [R] flag is redundant)
1603 ^localpath(.*) http://otherhost/otherpath$1 [P] http://otherhost/otherpath/pathinfo
1610 <p><strong>Example:</strong></p>
1612 <p>We want to rewrite URLs of the form </p>
1615 <code>/</code> <em>Language</em> <code>/~</code>
1616 <em>Realname</em> <code>/.../</code> <em>File</em>
1622 <code>/u/</code> <em>Username</em> <code>/.../</code>
1623 <em>File</em> <code>.</code> <em>Language</em>
1626 <p>We take the rewrite mapfile from above and save it under
1627 <code>/path/to/file/map.txt</code>. Then we only have to
1628 add the following lines to the Apache server configuration
1631 <blockquote><table cellpadding="10"><tr><td bgcolor="#eeeeee"><code>
1633 RewriteLog /path/to/file/rewrite.log
1634 RewriteMap real-to-user txt:/path/to/file/map.txt
1635 RewriteRule ^/([^/]+)/~([^/]+)/(.*)$ /u/${real-to-user:$2|nobody}/$3.$1
1637 </code></td></tr></table></blockquote>
1638 <hr/></blockquote><h3 align="center">Apache HTTP Server Version 2.0</h3><a href="./"><img src="../images/index.gif" alt="Index"/></a><a href="../"><img src="../images/home.gif" alt="Home"/></a></body></html>