1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
4 <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" />
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7 This file is generated from xml source: DO NOT EDIT
8 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
10 <title>mod_rewrite - Apache HTTP Server Version 2.5</title>
11 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
12 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
13 <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" />
14 <script src="../style/scripts/prettify.min.js" type="text/javascript">
17 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
19 <div id="page-header">
20 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
21 <p class="apache">Apache HTTP Server Version 2.5</p>
22 <img alt="" src="../images/feather.gif" /></div>
23 <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
25 <a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.5</a> > <a href="./">Modules</a></div>
26 <div id="page-content">
27 <div id="preamble"><h1>Apache Module mod_rewrite</h1>
29 <p><span>Available Languages: </span><a href="../en/mod/mod_rewrite.html" title="English"> en </a> |
30 <a href="../fr/mod/mod_rewrite.html" hreflang="fr" rel="alternate" title="Français"> fr </a></p>
32 <table class="module"><tr><th><a href="module-dict.html#Description">Description:</a></th><td>Provides a rule-based rewriting engine to rewrite requested
33 URLs on the fly</td></tr>
34 <tr><th><a href="module-dict.html#Status">Status:</a></th><td>Extension</td></tr>
35 <tr><th><a href="module-dict.html#ModuleIdentifier">Module Identifier:</a></th><td>rewrite_module</td></tr>
36 <tr><th><a href="module-dict.html#SourceFile">Source File:</a></th><td>mod_rewrite.c</td></tr></table>
39 <p>The <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> module uses a rule-based rewriting
40 engine, based on a PCRE regular-expression parser, to rewrite requested URLs on
41 the fly. By default, <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> maps a URL to a filesystem
42 path. However, it can also be used to redirect one URL to another URL, or
43 to invoke an internal proxy fetch.</p>
44 <p><code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> provides a flexible and powerful way to
45 manipulate URLs using an unlimited number of rules. Each rule can have an
46 unlimited number of attached rule conditions, to allow you to rewrite URL
47 based on server variables, environment variables, HTTP headers, or time
49 <p><code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> operates on the full URL path, including the
50 path-info section. A rewrite rule can be invoked in
51 <code>httpd.conf</code> or in <code>.htaccess</code>. The path generated
52 by a rewrite rule can include a query string, or can lead to internal
53 sub-processing, external request redirection, or internal proxy
56 <p>Further details, discussion, and examples, are provided in the
57 <a href="../rewrite/">detailed mod_rewrite documentation</a>.</p>
59 <div id="quickview"><h3>Topics</h3>
61 <li><img alt="" src="../images/down.gif" /> <a href="#logging">Logging</a></li>
62 </ul><h3 class="directives">Directives</h3>
64 <li><img alt="" src="../images/down.gif" /> <a href="#rewritebase">RewriteBase</a></li>
65 <li><img alt="" src="../images/down.gif" /> <a href="#rewritecond">RewriteCond</a></li>
66 <li><img alt="" src="../images/down.gif" /> <a href="#rewriteengine">RewriteEngine</a></li>
67 <li><img alt="" src="../images/down.gif" /> <a href="#rewritemap">RewriteMap</a></li>
68 <li><img alt="" src="../images/down.gif" /> <a href="#rewriteoptions">RewriteOptions</a></li>
69 <li><img alt="" src="../images/down.gif" /> <a href="#rewriterule">RewriteRule</a></li>
71 <ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
72 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
74 <h2><a name="logging" id="logging">Logging</a></h2>
76 <p><code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> offers detailed logging of its actions
77 at the <code>trace1</code> to <code>trace8</code> log levels. The
78 log level can be set specifically for <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
79 using the <code class="directive"><a href="../mod/core.html#loglevel">LogLevel</a></code> directive: Up to
80 level <code>debug</code>, no actions are logged, while <code>trace8</code>
81 means that practically all actions are logged.</p>
84 Using a high trace log level for <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
85 will slow down your Apache HTTP Server dramatically! Use a log
86 level higher than <code>trace2</code> only for debugging!
89 <div class="example"><h3>Example</h3><pre class="prettyprint lang-config">LogLevel alert rewrite:trace3</pre>
92 <div class="note"><h3>RewriteLog</h3>
93 <p>Those familiar with earlier versions of
94 <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> will no doubt be looking for the
95 <code>RewriteLog</code> and <code>RewriteLogLevel</code>
96 directives. This functionality has been completely replaced by the
97 new per-module logging configuration mentioned above.
100 <p>To get just the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>-specific log
101 messages, pipe the log file through grep:</p>
102 <div class="example"><p><code>
103 tail -f error_log|fgrep '[rewrite:'
108 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
109 <div class="directive-section"><h2><a name="RewriteBase" id="RewriteBase">RewriteBase</a> <a name="rewritebase" id="rewritebase">Directive</a></h2>
110 <table class="directive">
111 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets the base URL for per-directory rewrites</td></tr>
112 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>RewriteBase <em>URL-path</em></code></td></tr>
113 <tr><th><a href="directive-dict.html#Default">Default:</a></th><td><code>None</code></td></tr>
114 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>directory, .htaccess</td></tr>
115 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
116 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Extension</td></tr>
117 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_rewrite</td></tr>
119 <p>The <code class="directive">RewriteBase</code> directive specifies the
120 URL prefix to be used for per-directory (htaccess)
121 <code class="directive">RewriteRule</code> directives that substitute a relative
123 <p> This directive is <em>required</em> when you use a relative path
124 in a substitution in per-directory (htaccess) context unless either
125 of the following conditions are true:</p>
127 <li> The original request, and the substitution, are underneath the
128 <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
129 (as opposed to reachable by other means, such as
130 <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code>).</li>
131 <li> The <em>filesystem</em> path to the directory containing the
132 <code class="directive">RewriteRule</code>, suffixed by the relative
133 substitution is also valid as a URL path on the server
135 <li> In Apache HTTP Server 2.4.11 and later, this directive may be
136 omitted when the request is mapped via
137 <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code>
138 or <code class="module"><a href="../mod/mod_userdir.html">mod_userdir</a></code>.</li>
141 <p> In the example below, <code class="directive">RewriteBase</code> is necessary
142 to avoid rewriting to http://example.com/opt/myapp-1.2.3/welcome.html
143 since the resource was not relative to the document root. This
144 misconfiguration would normally cause the server to look for an "opt"
145 directory under the document root.</p>
146 <pre class="prettyprint lang-config">DocumentRoot "/var/www/example.com"
147 AliasMatch "^/myapp" "/opt/myapp-1.2.3"
148 <Directory "/opt/myapp-1.2.3">
150 RewriteBase "/myapp/"
151 RewriteRule "^index\.html$" "welcome.html"
152 </Directory></pre>
157 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
158 <div class="directive-section"><h2><a name="RewriteCond" id="RewriteCond">RewriteCond</a> <a name="rewritecond" id="rewritecond">Directive</a></h2>
159 <table class="directive">
160 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Defines a condition under which rewriting will take place
162 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code> RewriteCond
163 <em>TestString</em> <em>CondPattern</em></code></td></tr>
164 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
165 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
166 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Extension</td></tr>
167 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_rewrite</td></tr>
169 <p>The <code class="directive">RewriteCond</code> directive defines a
170 rule condition. One or more <code class="directive">RewriteCond</code>
171 can precede a <code class="directive"><a href="#rewriterule">RewriteRule</a></code>
172 directive. The following rule is then only used if both
173 the current state of the URI matches its pattern, <strong>and</strong> if these conditions are met.</p>
175 <p><em>TestString</em> is a string which can contain the
176 following expanded constructs in addition to plain text:</p>
180 <strong>RewriteRule backreferences</strong>: These are
181 backreferences of the form <strong><code>$N</code></strong>
182 (0 <= N <= 9). $1 to $9 provide access to the grouped
183 parts (in parentheses) of the pattern, from the
184 <code>RewriteRule</code> which is subject to the current
185 set of <code>RewriteCond</code> conditions. $0 provides
186 access to the whole string matched by that pattern.
189 <strong>RewriteCond backreferences</strong>: These are
190 backreferences of the form <strong><code>%N</code></strong>
191 (0 <= N <= 9). %1 to %9 provide access to the grouped
192 parts (again, in parentheses) of the pattern, from the last matched
193 <code>RewriteCond</code> in the current set
194 of conditions. %0 provides access to the whole string matched by
198 <strong>RewriteMap expansions</strong>: These are
199 expansions of the form <strong><code>${mapname:key|default}</code></strong>.
200 See <a href="#mapfunc">the documentation for
201 RewriteMap</a> for more details.
204 <strong>Server-Variables</strong>: These are variables of
206 <strong><code>%{</code> <em>NAME_OF_VARIABLE</em>
207 <code>}</code></strong>
208 where <em>NAME_OF_VARIABLE</em> can be a string taken
209 from the following list:
214 <th>HTTP headers:</th> <th>connection & request:</th> <th />
223 HTTP_PROXY_CONNECTION<br />
225 HTTP_USER_AGENT<br />
230 CONN_REMOTE_ADDR<br />
232 CONTEXT_DOCUMENT_ROOT<br />
242 SCRIPT_FILENAME<br />
249 <th>server internals:</th> <th>date and time:</th> <th>specials:</th>
261 SERVER_PROTOCOL<br />
262 SERVER_SOFTWARE<br />
278 CONN_REMOTE_ADDR<br />
282 REQUEST_FILENAME<br />
290 <p>These variables all
291 correspond to the similarly named HTTP
292 MIME-headers, C variables of the Apache HTTP Server or
293 <code>struct tm</code> fields of the Unix system.
294 Most are documented <a href="../expr.html#vars">here</a>
295 or elsewhere in the Manual or in the CGI specification.</p>
297 <p>SERVER_NAME and SERVER_PORT depend on the values of
298 <code class="directive"><a href="../mod/core.html#usecanonicalname">UseCanonicalName</a></code> and
299 <code class="directive"><a href="../mod/core.html#usecanonicalphysicalport">UseCanonicalPhysicalPort</a></code>
302 <p>Those that are special to mod_rewrite include those below.</p>
305 <dt><code>API_VERSION</code></dt>
307 <dd>This is the version of the Apache httpd module API
308 (the internal interface between server and
309 module) in the current httpd build, as defined in
310 include/ap_mmn.h. The module API version
311 corresponds to the version of Apache httpd in use (in
312 the release version of Apache httpd 1.3.14, for
313 instance, it is 19990320:10), but is mainly of
314 interest to module authors.</dd>
316 <dt><code>CONN_REMOTE_ADDR</code></dt>
318 <dd>Since 2.4.8: The peer IP address of the connection (see the
319 <code class="module"><a href="../mod/mod_remoteip.html">mod_remoteip</a></code> module).</dd>
321 <dt><code>HTTPS</code></dt>
323 <dd>Will contain the text "on" if the connection is
324 using SSL/TLS, or "off" otherwise. (This variable
325 can be safely used regardless of whether or not
326 <code class="module"><a href="../mod/mod_ssl.html">mod_ssl</a></code> is loaded).</dd>
328 <dt><code>IS_SUBREQ</code></dt>
330 <dd>Will contain the text "true" if the request
331 currently being processed is a sub-request,
332 "false" otherwise. Sub-requests may be generated
333 by modules that need to resolve additional files
334 or URIs in order to complete their tasks.</dd>
336 <dt><code>REMOTE_ADDR</code></dt>
338 <dd>The IP address of the remote host (see the
339 <code class="module"><a href="../mod/mod_remoteip.html">mod_remoteip</a></code> module).</dd>
341 <dt><code>REQUEST_FILENAME</code></dt>
343 <dd>The full local filesystem path to the file or
344 script matching the request, if this has already
345 been determined by the server at the time
346 <code>REQUEST_FILENAME</code> is referenced. Otherwise,
347 such as when used in virtual host context, the same
348 value as <code>REQUEST_URI</code>. Depending on the value of
349 <code class="directive"><a href="../mod/core.html#acceptpathinfo">AcceptPathInfo</a></code>, the
350 server may have only used some leading components of the
351 <code>REQUEST_URI</code> to map the request to a file.
354 <dt><code>REQUEST_SCHEME</code></dt>
356 <dd>Will contain the scheme of the request (usually
357 "http" or "https"). This value can be influenced with
358 <code class="directive"><a href="../mod/core.html#servername">ServerName</a></code>.</dd>
360 <dt><code>REQUEST_URI</code></dt>
362 <dd>The path component of the requested URI,
363 such as "/index.html". This notably excludes the
364 query string which is available as its own variable
365 named <code>QUERY_STRING</code>.</dd>
367 <dt><code>THE_REQUEST</code></dt>
369 <dd>The full HTTP request line sent by the
370 browser to the server (e.g., "<code>GET
371 /index.html HTTP/1.1</code>"). This does not
372 include any additional headers sent by the
373 browser. This value has not been unescaped
374 (decoded), unlike most other variables below.</dd>
381 <p>If the <em>TestString</em> has the special value <code>expr</code>,
382 the <em>CondPattern</em> will be treated as an
383 <a href="../expr.html">ap_expr</a>. HTTP headers referenced in the
384 expression will be added to the Vary header if the <code>novary</code>
385 flag is not given.</p>
387 <p>Other things you should be aware of:</p>
391 <p>The variables SCRIPT_FILENAME and REQUEST_FILENAME
392 contain the same value - the value of the
393 <code>filename</code> field of the internal
394 <code>request_rec</code> structure of the Apache HTTP Server.
395 The first name is the commonly known CGI variable name
396 while the second is the appropriate counterpart of
397 REQUEST_URI (which contains the value of the
398 <code>uri</code> field of <code>request_rec</code>).</p>
399 <p>If a substitution occurred and the rewriting continues,
400 the value of both variables will be updated accordingly.</p>
401 <p>If used in per-server context (<em>i.e.</em>, before the
402 request is mapped to the filesystem) SCRIPT_FILENAME and
403 REQUEST_FILENAME cannot contain the full local filesystem
404 path since the path is unknown at this stage of processing.
405 Both variables will initially contain the value of REQUEST_URI
406 in that case. In order to obtain the full local filesystem
407 path of the request in per-server context, use an URL-based
408 look-ahead <code>%{LA-U:REQUEST_FILENAME}</code> to determine
409 the final value of REQUEST_FILENAME.</p></li>
412 <code>%{ENV:variable}</code>, where <em>variable</em> can be
413 any environment variable, is also available.
414 This is looked-up via internal
415 Apache httpd structures and (if not found there) via
416 <code>getenv()</code> from the Apache httpd server process.</li>
419 <code>%{SSL:variable}</code>, where <em>variable</em> is the
420 name of an <a href="mod_ssl.html#envvars">SSL environment
421 variable</a>, can be used whether or not
422 <code class="module"><a href="../mod/mod_ssl.html">mod_ssl</a></code> is loaded, but will always expand to
423 the empty string if it is not. Example:
424 <code>%{SSL:SSL_CIPHER_USEKEYSIZE}</code> may expand to
425 <code>128</code>. These variables are available even without
426 setting the <code>StdEnvVars</code> option of the
427 <code class="directive"><a href="../mod/mod_ssl.html#ssloptions">SSLOptions</a></code> directive.</li>
430 <code>%{HTTP:header}</code>, where <em>header</em> can be
431 any HTTP MIME-header name, can always be used to obtain the
432 value of a header sent in the HTTP request.
433 Example: <code>%{HTTP:Proxy-Connection}</code> is
434 the value of the HTTP header
435 ``<code>Proxy-Connection:</code>''.
436 <p>If a HTTP header is used in a condition this header is added to
437 the Vary header of the response in case the condition evaluates
438 to true for the request. It is <strong>not</strong> added if the
439 condition evaluates to false for the request. Adding the HTTP header
440 to the Vary header of the response is needed for proper caching.</p>
441 <p>It has to be kept in mind that conditions follow a short circuit
442 logic in the case of the '<strong><code>ornext|OR</code></strong>' flag
443 so that certain conditions might not be evaluated at all.</p></li>
446 <a id="LA-U" name="LA-U"><code>%{LA-U:variable}</code></a>
447 can be used for look-aheads which perform
448 an internal (URL-based) sub-request to determine the final
449 value of <em>variable</em>. This can be used to access
450 variable for rewriting which is not available at the current
451 stage, but will be set in a later phase.
452 <p>For instance, to rewrite according to the
453 <code>REMOTE_USER</code> variable from within the
454 per-server context (<code>httpd.conf</code> file) you must
455 use <code>%{LA-U:REMOTE_USER}</code> - this
456 variable is set by the authorization phases, which come
457 <em>after</em> the URL translation phase (during which mod_rewrite
459 <p>On the other hand, because mod_rewrite implements
460 its per-directory context (<code>.htaccess</code> file) via
461 the Fixup phase of the API and because the authorization
462 phases come <em>before</em> this phase, you just can use
463 <code>%{REMOTE_USER}</code> in that context.</p></li>
466 <code>%{LA-F:variable}</code> can be used to perform an internal
467 (filename-based) sub-request, to determine the final value
468 of <em>variable</em>. Most of the time, this is the same as
472 <p><em>CondPattern</em> is the condition pattern,
473 a regular expression which is applied to the
474 current instance of the <em>TestString</em>.
475 <em>TestString</em> is first evaluated, before being matched against
476 <em>CondPattern</em>.</p>
478 <p><em>CondPattern</em> is usually a
479 <em>perl compatible regular expression</em>, but there is
480 additional syntax available to perform other useful tests against
481 the <em>Teststring</em>:</p>
484 <li>You can prefix the pattern string with a
485 '<code>!</code>' character (exclamation mark) to negate the result
486 of the condition, no matter what kind of <em>CondPattern</em> is used.
490 You can perform lexicographical string comparisons:
493 <li>'<strong><CondPattern</strong>' (lexicographically
495 Treats the <em>CondPattern</em> as a plain string and
496 compares it lexicographically to <em>TestString</em>. True if
497 <em>TestString</em> lexicographically precedes
498 <em>CondPattern</em>.</li>
500 <li>'<strong>>CondPattern</strong>' (lexicographically
502 Treats the <em>CondPattern</em> as a plain string and
503 compares it lexicographically to <em>TestString</em>. True if
504 <em>TestString</em> lexicographically follows
505 <em>CondPattern</em>.</li>
507 <li>'<strong>=CondPattern</strong>' (lexicographically
509 Treats the <em>CondPattern</em> as a plain string and
510 compares it lexicographically to <em>TestString</em>. True if
511 <em>TestString</em> is lexicographically equal to
512 <em>CondPattern</em> (the two strings are exactly
513 equal, character for character). If <em>CondPattern</em>
514 is <code>""</code> (two quotation marks) this
515 compares <em>TestString</em> to the empty string.</li>
517 <li>'<strong><=CondPattern</strong>' (lexicographically
518 less than or equal to)<br />
519 Treats the <em>CondPattern</em> as a plain string and
520 compares it lexicographically to <em>TestString</em>. True
521 if <em>TestString</em> lexicographically precedes
522 <em>CondPattern</em>, or is equal to <em>CondPattern</em>
523 (the two strings are equal, character for character).</li>
525 <li>'<strong>>=CondPattern</strong>' (lexicographically
526 greater than or equal to)<br />
527 Treats the <em>CondPattern</em> as a plain string and
528 compares it lexicographically to <em>TestString</em>. True
529 if <em>TestString</em> lexicographically follows
530 <em>CondPattern</em>, or is equal to <em>CondPattern</em>
531 (the two strings are equal, character for character).</li>
535 You can perform integer comparisons:
538 <li>'<strong>-eq</strong>' (is numerically
539 <strong>eq</strong>ual to)<br />
540 The <em>TestString</em> is treated as an integer, and is
541 numerically compared to the <em>CondPattern</em>. True if
542 the two are numerically equal.</li>
544 <li>'<strong>-ge</strong>' (is numerically
545 <strong>g</strong>reater than or <strong>e</strong>qual to)<br />
546 The <em>TestString</em> is treated as an integer, and is
547 numerically compared to the <em>CondPattern</em>. True if
548 the <em>TestString</em> is numerically greater than or equal
549 to the <em>CondPattern</em>.</li>
551 <li>'<strong>-gt</strong>' (is numerically
552 <strong>g</strong>reater <strong>t</strong>han)<br />
553 The <em>TestString</em> is treated as an integer, and is
554 numerically compared to the <em>CondPattern</em>. True if
555 the <em>TestString</em> is numerically greater than
556 the <em>CondPattern</em>.</li>
558 <li>'<strong>-le</strong>' (is numerically
559 <strong>l</strong>ess than or <strong>e</strong>qual to)<br />
560 The <em>TestString</em> is treated as an integer, and is
561 numerically compared to the <em>CondPattern</em>. True if
562 the <em>TestString</em> is numerically less than or equal
563 to the <em>CondPattern</em>. Avoid confusion with the
564 <strong>-l</strong> by using the <strong>-L</strong> or
565 <strong>-h</strong> variant.</li>
567 <li>'<strong>-lt</strong>' (is numerically
568 <strong>l</strong>ess <strong>t</strong>han)<br />
569 The <em>TestString</em> is treated as an integer, and is
570 numerically compared to the <em>CondPattern</em>. True if
571 the <em>TestString</em> is numerically less than
572 the <em>CondPattern</em>. Avoid confusion with the
573 <strong>-l</strong> by using the <strong>-L</strong> or
574 <strong>-h</strong> variant.</li>
579 <li>You can perform various file attribute tests:
581 <li>'<strong>-d</strong>' (is
582 <strong>d</strong>irectory)<br />
583 Treats the <em>TestString</em> as a pathname and tests
584 whether or not it exists, and is a directory.</li>
586 <li>'<strong>-f</strong>' (is regular
587 <strong>f</strong>ile)<br />
588 Treats the <em>TestString</em> as a pathname and tests
589 whether or not it exists, and is a regular file.</li>
591 <li>'<strong>-F</strong>' (is existing file, via
593 Checks whether or not <em>TestString</em> is a valid file,
594 accessible via all the server's currently-configured
595 access controls for that path. This uses an internal
596 subrequest to do the check, so use it with care -
597 it can impact your server's performance!</li>
599 <li>'<strong>-H</strong>' (is symbolic link, bash convention)<br />
600 See <strong>-l</strong>.</li>
602 <li>'<strong>-l</strong>' (is symbolic
603 <strong>l</strong>ink)<br />
604 Treats the <em>TestString</em> as a pathname and tests
605 whether or not it exists, and is a symbolic link. May also
606 use the bash convention of <strong>-L</strong> or
607 <strong>-h</strong> if there's a possibility of confusion
608 such as when using the <strong>-lt</strong> or
609 <strong>-le</strong> tests.</li>
611 <li>'<strong>-L</strong>' (is symbolic link, bash convention)<br />
612 See <strong>-l</strong>.</li>
614 <li>'<strong>-s</strong>' (is regular file, with
615 <strong>s</strong>ize)<br />
616 Treats the <em>TestString</em> as a pathname and tests
617 whether or not it exists, and is a regular file with size greater
620 <li><p>'<strong>-U</strong>' (is existing URL, via
622 Checks whether or not <em>TestString</em> is a valid URL,
623 accessible via all the server's currently-configured
624 access controls for that path. This uses an internal
625 subrequest to do the check, so use it with care -
626 it can impact your server's performance!</p>
627 <p> This flag <em>only</em> returns information about things
628 like access control, authentication, and authorization. This flag
629 <em>does not</em> return information about the status code the
630 configured handler (static file, CGI, proxy, etc.) would have
633 <li>'<strong>-x</strong>' (has e<strong>x</strong>ecutable
635 Treats the <em>TestString</em> as a pathname and tests
636 whether or not it exists, and has executable permissions.
637 These permissions are determined according to
638 the underlying OS.</li>
645 <p>If the <em>TestString</em> has the special value <code>expr</code>, the
646 <em>CondPattern</em> will be treated as an
647 <a href="../expr.html">ap_expr</a>.</p>
650 In the below example, <code>-strmatch</code> is used to
651 compare the <code>REFERER</code> against the site hostname,
652 to block unwanted hotlinking.
655 <pre class="prettyprint lang-config">RewriteCond expr "! %{HTTP_REFERER} -strmatch '*://%{HTTP_HOST}/*'"<br />
656 RewriteRule "^/images" "-" [F]</pre>
660 <li>You can also set special flags for
661 <em>CondPattern</em> by appending
662 <strong><code>[</code><em>flags</em><code>]</code></strong>
663 as the third argument to the <code>RewriteCond</code>
664 directive, where <em>flags</em> is a comma-separated list of any of the
668 <li>'<strong><code>nocase|NC</code></strong>'
669 (<strong>n</strong>o <strong>c</strong>ase)<br />
670 This makes the test case-insensitive - differences
671 between 'A-Z' and 'a-z' are ignored, both in the
672 expanded <em>TestString</em> and the <em>CondPattern</em>.
673 This flag is effective only for comparisons between
674 <em>TestString</em> and <em>CondPattern</em>. It has no
675 effect on filesystem and subrequest checks.</li>
678 '<strong><code>ornext|OR</code></strong>'
679 (<strong>or</strong> next condition)<br />
680 Use this to combine rule conditions with a local OR
681 instead of the implicit AND. Typical example:
683 <pre class="prettyprint lang-config">RewriteCond "%{REMOTE_HOST}" "^host1" [OR]
684 RewriteCond "%{REMOTE_HOST}" "^host2" [OR]
685 RewriteCond "%{REMOTE_HOST}" "^host3"
686 RewriteRule ...some special stuff for any of these hosts...</pre>
689 Without this flag you would have to write the condition/rule
693 <li>'<strong><code>novary|NV</code></strong>'
694 (<strong>n</strong>o <strong>v</strong>ary)<br />
695 If a HTTP header is used in the condition, this flag prevents
696 this header from being added to the Vary header of the response. <br />
697 Using this flag might break proper caching of the response if
698 the representation of this response varies on the value of this header.
699 So this flag should be only used if the meaning of the Vary header
706 <p><strong>Example:</strong></p>
708 <p>To rewrite the Homepage of a site according to the
709 ``<code>User-Agent:</code>'' header of the request, you can
710 use the following: </p>
712 <pre class="prettyprint lang-config">RewriteCond "%{HTTP_USER_AGENT}" "(iPhone|Blackberry|Android)"
713 RewriteRule "^/$" "/homepage.mobile.html" [L]
715 RewriteRule "^/$" "/homepage.std.html" [L]</pre>
718 <p>Explanation: If you use a browser which identifies itself
719 as a mobile browser (note that the example is incomplete, as
720 there are many other mobile platforms), the mobile version of
721 the homepage is served. Otherwise, the standard page is served.
726 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
727 <div class="directive-section"><h2><a name="RewriteEngine" id="RewriteEngine">RewriteEngine</a> <a name="rewriteengine" id="rewriteengine">Directive</a></h2>
728 <table class="directive">
729 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Enables or disables runtime rewriting engine</td></tr>
730 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>RewriteEngine on|off</code></td></tr>
731 <tr><th><a href="directive-dict.html#Default">Default:</a></th><td><code>RewriteEngine off</code></td></tr>
732 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
733 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
734 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Extension</td></tr>
735 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_rewrite</td></tr>
738 <p>The <code class="directive">RewriteEngine</code> directive enables or
739 disables the runtime rewriting engine. If it is set to
740 <code>off</code> this module does no runtime processing at
741 all. It does not even update the <code>SCRIPT_URx</code>
742 environment variables.</p>
744 <p>Use this directive to disable rules in a particular context,
745 rather than commenting out all the <code class="directive"><a href="#rewriterule">RewriteRule</a></code> directives.</p>
747 <p>Note that rewrite configurations are not
748 inherited by virtual hosts. This means that you need to have a
749 <code>RewriteEngine on</code> directive for each virtual host
750 in which you wish to use rewrite rules.</p>
752 <p><code class="directive">RewriteMap</code> directives of the type <code>prg</code>
753 are not started during server initialization if they're defined in a
754 context that does not have <code class="directive">RewriteEngine</code> set to
758 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
759 <div class="directive-section"><h2><a name="RewriteMap" id="RewriteMap">RewriteMap</a> <a name="rewritemap" id="rewritemap">Directive</a></h2>
760 <table class="directive">
761 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Defines a mapping function for key-lookup</td></tr>
762 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>RewriteMap <em>MapName</em> <em>MapType</em>:<em>MapSource</em>
763 <em>MapTypeOptions</em>
765 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host</td></tr>
766 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Extension</td></tr>
767 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_rewrite</td></tr>
769 <p>The <code class="directive">RewriteMap</code> directive defines a
770 <em>Rewriting Map</em> which can be used inside rule
771 substitution strings by the mapping-functions to
772 insert/substitute fields through a key lookup. The source of
773 this lookup can be of various types.</p>
775 <p>The <a id="mapfunc" name="mapfunc"><em>MapName</em></a> is
776 the name of the map and will be used to specify a
777 mapping-function for the substitution strings of a rewriting
778 rule via one of the following constructs:</p>
781 <strong><code>${</code> <em>MapName</em> <code>:</code>
782 <em>LookupKey</em> <code>}</code><br />
783 <code>${</code> <em>MapName</em> <code>:</code>
784 <em>LookupKey</em> <code>|</code> <em>DefaultValue</em>
785 <code>}</code></strong>
788 <p>When such a construct occurs, the map <em>MapName</em> is
789 consulted and the key <em>LookupKey</em> is looked-up. If the
790 key is found, the map-function construct is substituted by
791 <em>SubstValue</em>. If the key is not found then it is
792 substituted by <em>DefaultValue</em> or by the empty string
793 if no <em>DefaultValue</em> was specified. Empty values
794 behave as if the key was absent, therefore it is not possible
795 to distinguish between empty-valued keys and absent keys.</p>
797 <p>For example, you might define a
798 <code class="directive">RewriteMap</code> as:</p>
800 <pre class="prettyprint lang-config">RewriteMap examplemap "txt:/path/to/file/map.txt"</pre>
803 <p>You would then be able to use this map in a
804 <code class="directive">RewriteRule</code> as follows:</p>
806 <pre class="prettyprint lang-config">RewriteRule "^/ex/(.*)" "${examplemap:$1}"</pre>
809 <p>The meaning of the <em>MapTypeOptions</em> argument depends on
810 particular <em>MapType</em>. See the
811 <a href="../rewrite/rewritemap.html">Using RewriteMap</a> for
812 more information.</p>
814 <p>The following combinations for <em>MapType</em> and
815 <em>MapSource</em> can be used:</p>
820 <dd>A plain text file containing space-separated key-value
821 pairs, one per line. (<a href="../rewrite/rewritemap.html#txt">Details ...</a>)</dd>
824 <dd>Randomly selects an entry from a plain text file (<a href="../rewrite/rewritemap.html#rnd">Details ...</a>)</dd>
827 <dd>Looks up an entry in a dbm file containing name, value
828 pairs. Hash is constructed from a plain text file format using
829 the <code><a href="../programs/httxt2dbm.html">httxt2dbm</a></code>
830 utility. (<a href="../rewrite/rewritemap.html#dbm">Details ...</a>)</dd>
833 <dd>One of the four available internal functions provided by
834 <code>RewriteMap</code>: toupper, tolower, escape or
835 unescape. (<a href="../rewrite/rewritemap.html#int">Details ...</a>)</dd>
838 <dd>Calls an external program or script to process the
839 rewriting. (<a href="../rewrite/rewritemap.html#prg">Details ...</a>)</dd>
841 <dt>dbd or fastdbd</dt>
842 <dd>A SQL SELECT statement to be performed to look up the
843 rewrite target. (<a href="../rewrite/rewritemap.html#dbd">Details ...</a>)</dd>
846 <p>Further details, and numerous examples, may be found in the <a href="../rewrite/rewritemap.html">RewriteMap HowTo</a></p>
850 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
851 <div class="directive-section"><h2><a name="RewriteOptions" id="RewriteOptions">RewriteOptions</a> <a name="rewriteoptions" id="rewriteoptions">Directive</a></h2>
852 <table class="directive">
853 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets some special options for the rewrite engine</td></tr>
854 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>RewriteOptions <var>Options</var></code></td></tr>
855 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
856 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
857 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Extension</td></tr>
858 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_rewrite</td></tr>
861 <p>The <code class="directive">RewriteOptions</code> directive sets some
862 special options for the current per-server or per-directory
863 configuration. The <em>Option</em> string can currently
864 only be one of the following:</p>
867 <dt><code>Inherit</code></dt>
870 <p>This forces the current configuration to inherit the
871 configuration of the parent. In per-virtual-server context,
872 this means that the maps, conditions and rules of the main
873 server are inherited. In per-directory context this means
874 that conditions and rules of the parent directory's
875 <code>.htaccess</code> configuration or
876 <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code>
877 sections are inherited. The inherited rules are virtually copied
878 to the section where this directive is being used. If used in
879 combination with local rules, the inherited rules are copied behind
880 the local rules. The position of this directive - below or above
881 of local rules - has no influence on this behavior. If local
882 rules forced the rewriting to stop, the inherited rules won't
885 <div class="warning">
886 Rules inherited from the parent scope are applied
887 <strong>after</strong> rules specified in the child scope.
891 <dt><code>InheritBefore</code></dt>
893 <p> Like <code>Inherit</code> above, but the rules from the parent scope
894 are applied <strong>before</strong> rules specified in the child scope.<br />
895 Available in Apache HTTP Server 2.3.10 and later.</p>
898 <dt><code>InheritDown</code></dt>
901 <p>If this option is enabled, all child configurations will inherit
902 the configuration of the current configuration. It is equivalent to
903 specifying <code>RewriteOptions Inherit</code> in all child
904 configurations. See the <code>Inherit</code> option for more details
905 on how the parent-child relationships are handled.<br />
906 Available in Apache HTTP Server 2.4.8 and later.</p>
909 <dt><code>InheritDownBefore</code></dt>
912 <p>Like <code>InheritDown</code> above, but the rules from the current
913 scope are applied <strong>before</strong> rules specified in any child's
915 Available in Apache HTTP Server 2.4.8 and later.</p>
918 <dt><code>IgnoreInherit</code></dt>
921 <p>This option forces the current and child configurations to ignore
922 all rules that would be inherited from a parent specifying
923 <code>InheritDown</code> or <code>InheritDownBefore</code>.<br />
924 Available in Apache HTTP Server 2.4.8 and later.</p>
927 <dt><code>AllowNoSlash</code></dt>
929 <p>By default, <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> will ignore URLs that map to a
930 directory on disk but lack a trailing slash, in the expectation that
931 the <code class="module"><a href="../mod/mod_dir.html">mod_dir</a></code> module will issue the client with a redirect to
932 the canonical URL with a trailing slash.</p>
934 <p>When the <code class="directive"><a href="../mod/mod_dir.html#directoryslash">DirectorySlash</a></code> directive
935 is set to off, the <code>AllowNoSlash</code> option can be enabled to ensure
936 that rewrite rules are no longer ignored. This option makes it possible to
937 apply rewrite rules within .htaccess files that match the directory without
938 a trailing slash, if so desired.<br />
939 Available in Apache HTTP Server 2.4.0 and later.</p>
942 <dt><code>AllowAnyURI</code></dt>
945 <p>When <code class="directive"><a href="#rewriterule">RewriteRule</a></code>
946 is used in <code>VirtualHost</code> or server context with
947 version 2.2.22 or later of httpd, <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
948 will only process the rewrite rules if the request URI is a <a href="directive-dict.html#Syntax">URL-path</a>. This avoids
949 some security issues where particular rules could allow
950 "surprising" pattern expansions (see <a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2011-3368">CVE-2011-3368</a>
951 and <a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2011-4317">CVE-2011-4317</a>).
952 To lift the restriction on matching a URL-path, the
953 <code>AllowAnyURI</code> option can be enabled, and
954 <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> will apply the rule set to any
955 request URI string, regardless of whether that string matches
956 the URL-path grammar required by the HTTP specification.<br />
957 Available in Apache HTTP Server 2.4.3 and later.</p>
959 <div class="warning">
960 <h3>Security Warning</h3>
962 <p>Enabling this option will make the server vulnerable to
963 security issues if used with rewrite rules which are not
964 carefully authored. It is <strong>strongly recommended</strong>
965 that this option is not used. In particular, beware of input
966 strings containing the '<code>@</code>' character which could
967 change the interpretation of the transformed URI, as per the
972 <dt><code>MergeBase</code></dt>
975 <p>With this option, the value of <code class="directive"><a href="#rewritebase">RewriteBase</a></code> is copied from where it's explicitly defined
976 into any sub-directory or sub-location that doesn't define its own
977 <code class="directive"><a href="#rewritebase">RewriteBase</a></code>. This was the
978 default behavior in 2.4.0 through 2.4.3, and the flag to restore it is
979 available Apache HTTP Server 2.4.4 and later.</p>
982 <dt><code>IgnoreContextInfo</code></dt>
985 <p>When a relative substitution is made
986 in directory (htaccess) context and <code class="directive"><a href="#rewritebase">RewriteBase</a></code> has not been set, this module uses some
987 extended URL and filesystem context information to change the
988 relative substitution back into a URL. Modules such as
989 <code class="module"><a href="../mod/mod_userdir.html">mod_userdir</a></code> and <code class="module"><a href="../mod/mod_alias.html">mod_alias</a></code>
990 supply this extended context info. </p>
996 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
997 <div class="directive-section"><h2><a name="RewriteRule" id="RewriteRule">RewriteRule</a> <a name="rewriterule" id="rewriterule">Directive</a></h2>
998 <table class="directive">
999 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Defines rules for the rewriting engine</td></tr>
1000 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>RewriteRule
1001 <em>Pattern</em> <em>Substitution</em> [<em>flags</em>]</code></td></tr>
1002 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1003 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
1004 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Extension</td></tr>
1005 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_rewrite</td></tr>
1007 <p>The <code class="directive">RewriteRule</code> directive is the real
1008 rewriting workhorse. The directive can occur more than once,
1009 with each instance defining a single rewrite rule. The
1010 order in which these rules are defined is important - this is the order
1011 in which they will be applied at run-time.</p>
1013 <p><a id="patterns" name="patterns"><em>Pattern</em></a> is
1014 a perl compatible <a id="regexp" name="regexp">regular
1015 expression</a>. On the first RewriteRule, it is matched against
1016 the (%-decoded) <a href="directive-dict.html#Syntax">URL-path</a>
1017 of the request, or, in per-directory context (see below), the URL
1018 path relative to that per-directory context. Subsequent patterns
1019 are matched against the output of the last matching RewriteRule.</p>
1021 <div class="note"><h3><a id="what_is_matched" name="what_is_matched">What is matched?</a></h3>
1022 <p>In <code class="directive"><a href="../mod/core.html#virtualhost">VirtualHost</a></code> context,
1023 The <em>Pattern</em> will initially be matched against the part of the
1024 URL after the hostname and port, and before the query string (e.g. "/app1/index.html").</p>
1026 <p>In <code class="directive"><a href="../mod/core.html#directory">Directory</a></code> and htaccess context,
1027 the <em>Pattern</em> will initially be matched against the
1028 <em>filesystem</em> path, after removing the prefix that led the server
1029 to the current <code class="directive">RewriteRule</code> (e.g. "app1/index.html"
1030 or "index.html" depending on where the directives are defined).</p>
1032 <p>If you wish to match against the hostname, port, or query string, use a
1033 <code class="directive"><a href="#rewritecond">RewriteCond</a></code> with the
1034 <code>%{HTTP_HOST}</code>, <code>%{SERVER_PORT}</code>, or
1035 <code>%{QUERY_STRING}</code> variables respectively.</p>
1039 <div class="note"><h3>Per-directory Rewrites</h3>
1041 <li>The rewrite engine may be used in <a href="../howto/htaccess.html">.htaccess</a> files and in <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code> sections, with some additional
1044 <li>To enable the rewrite engine in this context, you need to set
1045 "<code>RewriteEngine On</code>" <strong>and</strong>
1046 "<code>Options FollowSymLinks</code>" must be enabled. If your
1047 administrator has disabled override of <code>FollowSymLinks</code> for
1048 a user's directory, then you cannot use the rewrite engine. This
1049 restriction is required for security reasons.</li>
1051 <li>When using the rewrite engine in <code>.htaccess</code> files the
1052 per-directory prefix (which always is the same for a specific
1053 directory) is automatically <em>removed</em> for the RewriteRule pattern matching
1054 and automatically <em>added</em> after any relative (not starting with a
1055 slash or protocol name) substitution encounters the end of a rule set.
1056 See the <code class="directive"><a href="#rewritebase">RewriteBase</a></code>
1057 directive for more information regarding what prefix will be added back to
1058 relative substitutions.</li>
1060 <li> If you wish to match against the full URL-path in a per-directory
1061 (htaccess) RewriteRule, use the <code>%{REQUEST_URI}</code> variable in
1062 a <code class="directive">RewriteCond</code>.</li>
1064 <li>The removed prefix always ends with a slash, meaning the matching occurs against a string which
1065 <em>never</em> has a leading slash. Therefore, a <em>Pattern</em> with <code>^/</code> never
1066 matches in per-directory context.</li>
1068 <li>Although rewrite rules are syntactically permitted in <code class="directive"><a href="../mod/core.html#location"><Location></a></code> and <code class="directive"><a href="../mod/core.html#files"><Files></a></code> sections
1069 (including their regular expression counterparts), this
1070 should never be necessary and is unsupported. A likely feature
1071 to break in these contexts is relative substitutions.</li>
1075 <p>For some hints on <a class="glossarylink" href="../glossary.html#regex" title="see glossary">regular
1076 expressions</a>, see
1077 the <a href="../rewrite/intro.html#regex">mod_rewrite
1078 Introduction</a>.</p>
1080 <p>In mod_rewrite, the NOT character
1081 ('<code>!</code>') is also available as a possible pattern
1082 prefix. This enables you to negate a pattern; to say, for instance:
1083 ``<em>if the current URL does <strong>NOT</strong> match this
1084 pattern</em>''. This can be used for exceptional cases, where
1085 it is easier to match the negative pattern, or as a last
1088 <div class="note"><h3>Note</h3>
1089 When using the NOT character to negate a pattern, you cannot include
1090 grouped wildcard parts in that pattern. This is because, when the
1091 pattern does NOT match (ie, the negation matches), there are no
1092 contents for the groups. Thus, if negated patterns are used, you
1093 cannot use <code>$N</code> in the substitution string!
1096 <p>The <a id="rhs" name="rhs"><em>Substitution</em></a> of a
1097 rewrite rule is the string that replaces the original URL-path that
1098 was matched by <em>Pattern</em>. The <em>Substitution</em> may
1103 <dt>file-system path</dt>
1105 <dd>Designates the location on the file-system of the resource
1106 to be delivered to the client. Substitutions are only
1107 treated as a file-system path when the rule is configured in
1108 server (virtualhost) context and the first component of the
1109 path in the substitution exists in the file-system</dd>
1113 <dd>A <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>-relative path to the
1114 resource to be served. Note that <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
1115 tries to guess whether you have specified a file-system path
1116 or a URL-path by checking to see if the first segment of the
1117 path exists at the root of the file-system. For example, if
1118 you specify a <em>Substitution</em> string of
1119 <code>/www/file.html</code>, then this will be treated as a
1120 URL-path <em>unless</em> a directory named <code>www</code>
1121 exists at the root or your file-system (or, in the case of
1122 using rewrites in a <code>.htaccess</code> file, relative to
1123 your document root), in which case it will
1124 be treated as a file-system path. If you wish other
1125 URL-mapping directives (such as <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code>) to be applied to the
1126 resulting URL-path, use the <code>[PT]</code> flag as
1127 described below.</dd>
1129 <dt>Absolute URL</dt>
1131 <dd>If an absolute URL is specified,
1132 <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> checks to see whether the
1133 hostname matches the current host. If it does, the scheme and
1134 hostname are stripped out and the resulting path is treated as
1135 a URL-path. Otherwise, an external redirect is performed for
1136 the given URL. To force an external redirect back to the
1137 current host, see the <code>[R]</code> flag below.</dd>
1139 <dt><code>-</code> (dash)</dt>
1141 <dd>A dash indicates that no substitution should be performed
1142 (the existing path is passed through untouched). This is used
1143 when a flag (see below) needs to be applied without changing
1148 <p>In addition to plain text, the <em>Substitution</em> string can include</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>
1164 <p>Back-references are identifiers of the form
1165 <code>$</code><strong>N</strong>
1166 (<strong>N</strong>=0..9), which will be replaced
1167 by the contents of the <strong>N</strong>th group of the
1168 matched <em>Pattern</em>. The server-variables are the same
1169 as for the <em>TestString</em> of a <code>RewriteCond</code>
1170 directive. The mapping-functions come from the
1171 <code>RewriteMap</code> directive and are explained there.
1172 These three types of variables are expanded in the order above.</p>
1174 <p>Rewrite rules are applied to the results of previous rewrite
1175 rules, in the order in which they are defined
1176 in the config file. The URL-path or file-system path (see <a href="#what_is_matched">"What is matched?"</a>, above) is <strong>completely
1177 replaced</strong> by the <em>Substitution</em> and the
1178 rewriting process continues until all rules have been applied,
1179 or it is explicitly terminated by an
1180 <a href="../rewrite/flags.html#flag_l"><code><strong>L</strong></code> flag</a>,
1181 or other flag which implies immediate termination, such as
1182 <code><strong>END</strong></code> or
1183 <code><strong>F</strong></code>.</p>
1185 <div class="note"><h3>Modifying the Query String</h3>
1186 <p>By default, the query string is passed through unchanged. You
1187 can, however, create URLs in the substitution string containing
1188 a query string part. Simply use a question mark inside the
1189 substitution string to indicate that the following text should
1190 be re-injected into the query string. When you want to erase an
1191 existing query string, end the substitution string with just a
1192 question mark. To combine new and old query strings, use the
1193 <code>[QSA]</code> flag.</p>
1196 <p>Additionally you can set special <a name="rewriteflags" id="rewriteflags">actions</a> to be performed by
1197 appending <strong><code>[</code><em>flags</em><code>]</code></strong>
1198 as the third argument to the <code>RewriteRule</code>
1199 directive. <em>Flags</em> is a comma-separated list, surround by square
1200 brackets, of any of the flags in the following table. More
1201 details, and examples, for each flag, are available in the <a href="../rewrite/flags.html">Rewrite Flags document</a>.</p>
1203 <table class="bordered"><tr class="header"><th>Flag and syntax</th>
1208 <td>Escape non-alphanumeric characters in backreferences <em>before</em>
1209 applying the transformation. <em><a href="../rewrite/flags.html#flag_b">details ...</a></em></td>
1212 <td>backrefnoplus|BNP</td>
1213 <td>If backreferences are being escaped, spaces should be escaped to
1214 %20 instead of +. Useful when the backreference will be used in the
1215 path component rather than the query string.<em><a href="../rewrite/flags.html#flag_bnp">details ...</a></em></td>
1219 <td>Rule is chained to the following rule. If the rule fails,
1220 the rule(s) chained to it will be skipped. <em><a href="../rewrite/flags.html#flag_c">details ...</a></em></td>
1223 <td>cookie|CO=<em>NAME</em>:<em>VAL</em></td>
1224 <td>Sets a cookie in the client browser. Full syntax is:
1225 CO=<em>NAME</em>:<em>VAL</em>:<em>domain</em>[:<em>lifetime</em>[:<em>path</em>[:<em>secure</em>[:<em>httponly</em>]]]] <em><a href="../rewrite/flags.html#flag_co">details ...</a></em>
1229 <td>discardpath|DPI</td>
1230 <td>Causes the PATH_INFO portion of the rewritten URI to be
1231 discarded. <em><a href="../rewrite/flags.html#flag_dpi">details
1236 <td>Stop the rewriting process immediately and don't apply any
1237 more rules. Also prevents further execution of rewrite rules
1238 in per-directory and .htaccess context. (Available in 2.3.9 and later)
1239 <em><a href="../rewrite/flags.html#flag_end">details ...</a></em></td>
1242 <td>env|E=[!]<em>VAR</em>[:<em>VAL</em>]</td>
1243 <td>Causes an environment variable <em>VAR</em> to be set (to the
1244 value <em>VAL</em> if provided). The form !<em>VAR</em> causes
1245 the environment variable <em>VAR</em> to be unset.
1246 <em><a href="../rewrite/flags.html#flag_e">details ...</a></em></td>
1249 <td>forbidden|F</td>
1250 <td>Returns a 403 FORBIDDEN response to the client browser.
1251 <em><a href="../rewrite/flags.html#flag_f">details ...</a></em></td>
1255 <td>Returns a 410 GONE response to the client browser. <em><a href="../rewrite/flags.html#flag_g">details ...</a></em></td>
1258 <td>Handler|H=<em>Content-handler</em></td>
1259 <td>Causes the resulting URI to be sent to the specified
1260 <em>Content-handler</em> for processing. <em><a href="../rewrite/flags.html#flag_h">details ...</a></em></td>
1264 <td>Stop the rewriting process immediately and don't apply any
1265 more rules. Especially note caveats for per-directory and
1266 .htaccess context (see also the END flag). <em><a href="../rewrite/flags.html#flag_l">details ...</a></em></td>
1270 <td>Re-run the rewriting process, starting again with the first
1271 rule, using the result of the ruleset so far as a starting
1272 point. <em><a href="../rewrite/flags.html#flag_n">details
1277 <td>Makes the pattern comparison case-insensitive.
1278 <em><a href="../rewrite/flags.html#flag_nc">details ...</a></em></td>
1281 <td>noescape|NE</td>
1282 <td>Prevent mod_rewrite from applying hexcode escaping of
1283 special characters in the result of the rewrite. <em><a href="../rewrite/flags.html#flag_ne">details ...</a></em></td>
1286 <td>nosubreq|NS</td>
1287 <td>Causes a rule to be skipped if the current request is an
1288 internal sub-request. <em><a href="../rewrite/flags.html#flag_ns">details ...</a></em></td>
1292 <td>Force the substitution URL to be internally sent as a proxy
1293 request. <em><a href="../rewrite/flags.html#flag_p">details
1297 <td>passthrough|PT</td>
1298 <td>Forces the resulting URI to be passed back to the URL
1299 mapping engine for processing of other URI-to-filename
1300 translators, such as <code>Alias</code> or
1301 <code>Redirect</code>. <em><a href="../rewrite/flags.html#flag_pt">details ...</a></em></td>
1304 <td>qsappend|QSA</td>
1305 <td>Appends any query string from the original request URL to
1306 any query string created in the rewrite target.<em><a href="../rewrite/flags.html#flag_qsa">details ...</a></em></td>
1309 <td>qsdiscard|QSD</td>
1310 <td>Discard any query string attached to the incoming URI.
1311 <em><a href="../rewrite/flags.html#flag_qsd">details
1315 <td>redirect|R[=<em>code</em>]</td>
1316 <td>Forces an external redirect, optionally with the specified
1317 HTTP status code. <em><a href="../rewrite/flags.html#flag_r">details ...</a></em>
1321 <td>skip|S=<em>num</em></td>
1322 <td>Tells the rewriting engine to skip the next <em>num</em>
1323 rules if the current rule matches. <em><a href="../rewrite/flags.html#flag_s">details ...</a></em></td>
1326 <td>type|T=<em>MIME-type</em></td>
1327 <td>Force the <a class="glossarylink" href="../glossary.html#mime-type" title="see glossary">MIME-type</a> of the target file
1328 to be the specified type. <em><a href="../rewrite/flags.html#flag_t">details ...</a></em></td>
1332 <div class="note"><h3>Home directory expansion</h3>
1333 <p> When the substitution string begins with a string
1334 resembling "/~user" (via explicit text or backreferences), mod_rewrite performs
1335 home directory expansion independent of the presence or configuration
1336 of <code class="module"><a href="../mod/mod_userdir.html">mod_userdir</a></code>.</p>
1338 <p> This expansion does not occur when the <em>PT</em>
1339 flag is used on the <code class="directive"><a href="#rewriterule">RewriteRule</a></code>
1344 <p>Here are all possible substitution combinations and their
1347 <p><strong>Inside per-server configuration
1348 (<code>httpd.conf</code>)<br />
1349 for request ``<code>GET
1350 /somepath/pathinfo</code>'':</strong><br />
1353 <table class="bordered"><tr class="header">
1355 <th>Resulting Substitution</th>
1358 <td>^/somepath(.*) otherpath$1</td>
1359 <td>invalid, not supported</td>
1362 <td>^/somepath(.*) otherpath$1 [R]</td>
1363 <td>invalid, not supported</td>
1366 <td>^/somepath(.*) otherpath$1 [P]</td>
1367 <td>invalid, not supported</td>
1370 <td>^/somepath(.*) /otherpath$1</td>
1371 <td>/otherpath/pathinfo</td>
1374 <td>^/somepath(.*) /otherpath$1 [R]</td>
1375 <td>http://thishost/otherpath/pathinfo via external redirection</td>
1378 <td>^/somepath(.*) /otherpath$1 [P]</td>
1379 <td>doesn't make sense, not supported</td>
1382 <td>^/somepath(.*) http://thishost/otherpath$1</td>
1383 <td>/otherpath/pathinfo</td>
1386 <td>^/somepath(.*) http://thishost/otherpath$1 [R]</td>
1387 <td>http://thishost/otherpath/pathinfo via external redirection</td>
1390 <td>^/somepath(.*) http://thishost/otherpath$1 [P]</td>
1391 <td>doesn't make sense, not supported</td>
1394 <td>^/somepath(.*) http://otherhost/otherpath$1</td>
1395 <td>http://otherhost/otherpath/pathinfo via external redirection</td>
1398 <td>^/somepath(.*) http://otherhost/otherpath$1 [R]</td>
1399 <td>http://otherhost/otherpath/pathinfo via external redirection (the [R] flag is redundant)</td>
1402 <td>^/somepath(.*) http://otherhost/otherpath$1 [P]</td>
1403 <td>http://otherhost/otherpath/pathinfo via internal proxy</td>
1407 <p><strong>Inside per-directory configuration for
1408 <code>/somepath</code><br />
1409 (<code>/physical/path/to/somepath/.htaccess</code>, with
1410 <code>RewriteBase "/somepath"</code>)<br />
1411 for request ``<code>GET
1412 /somepath/localpath/pathinfo</code>'':</strong><br />
1415 <table class="bordered"><tr class="header">
1417 <th>Resulting Substitution</th>
1420 <td>^localpath(.*) otherpath$1</td>
1421 <td>/somepath/otherpath/pathinfo</td>
1424 <td>^localpath(.*) otherpath$1 [R]</td>
1425 <td>http://thishost/somepath/otherpath/pathinfo via external
1429 <td>^localpath(.*) otherpath$1 [P]</td>
1430 <td>doesn't make sense, not supported</td>
1433 <td>^localpath(.*) /otherpath$1</td>
1434 <td>/otherpath/pathinfo</td>
1437 <td>^localpath(.*) /otherpath$1 [R]</td>
1438 <td>http://thishost/otherpath/pathinfo via external redirection</td>
1441 <td>^localpath(.*) /otherpath$1 [P]</td>
1442 <td>doesn't make sense, not supported</td>
1445 <td>^localpath(.*) http://thishost/otherpath$1</td>
1446 <td>/otherpath/pathinfo</td>
1449 <td>^localpath(.*) http://thishost/otherpath$1 [R]</td>
1450 <td>http://thishost/otherpath/pathinfo via external redirection</td>
1453 <td>^localpath(.*) http://thishost/otherpath$1 [P]</td>
1454 <td>doesn't make sense, not supported</td>
1457 <td>^localpath(.*) http://otherhost/otherpath$1</td>
1458 <td>http://otherhost/otherpath/pathinfo via external redirection</td>
1461 <td>^localpath(.*) http://otherhost/otherpath$1 [R]</td>
1462 <td>http://otherhost/otherpath/pathinfo via external redirection (the [R] flag is redundant)</td>
1465 <td>^localpath(.*) http://otherhost/otherpath$1 [P]</td>
1466 <td>http://otherhost/otherpath/pathinfo via internal proxy</td>
1473 <div class="bottomlang">
1474 <p><span>Available Languages: </span><a href="../en/mod/mod_rewrite.html" title="English"> en </a> |
1475 <a href="../fr/mod/mod_rewrite.html" hreflang="fr" rel="alternate" title="Français"> fr </a></p>
1476 </div><div class="top"><a href="#page-header"><img src="../images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
1477 <script type="text/javascript"><!--//--><![CDATA[//><!--
1478 var comments_shortname = 'httpd';
1479 var comments_identifier = 'http://httpd.apache.org/docs/trunk/mod/mod_rewrite.html';
1481 if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
1482 d.write('<div id="comments_thread"><\/div>');
1483 var s = d.createElement('script');
1484 s.type = 'text/javascript';
1486 s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
1487 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
1490 d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
1492 })(window, document);
1493 //--><!]]></script></div><div id="footer">
1494 <p class="apache">Copyright 2016 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
1495 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
1496 if (typeof(prettyPrint) !== 'undefined') {