<code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> alternative for <code>.htaccess</code>
use.</p>
+ <div class="note"><h3>Processing order</h3>
+ <p>If you do mix <code class="directive"><a href="../mod/mod_alias.html#redirect">Redirect</a></code>
+ and <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> in the
+ same context, be aware that their execution order depends on where
+ they appear. In server/virtual-host context,
+ <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> runs first; in per-directory context
+ (<code>.htaccess</code>), <code class="module"><a href="../mod/mod_alias.html">mod_alias</a></code> runs first. See
+ <a href="tech.html#order">Module Processing Order</a> for
+ details.</p>
+ </div>
+
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="alias" id="alias">URL Aliasing</a> <a title="Permanent link" href="#alias" class="permalink">¶</a></h2>
<p>The <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code> directive
-provides mapping from a URI to a directory - usually a directory outside
+provides mapping from a URL-path to a directory - usually a directory outside
of your <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>. Although it
is possible to perform this mapping with <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>,
<code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code> is the preferred method, for
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.de.xsl"?>
-<!-- English Revision: 1933071:1934122 (outdated) -->
+<!-- English Revision: 1933071:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.es.xsl"?>
-<!-- English Revision: 1933071:1934122 (outdated) -->
+<!-- English Revision: 1933071:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.fr.xsl"?>
-<!-- English Revision: 1933071:1934122 (outdated) -->
+<!-- English Revision: 1933071:1934233 (outdated) -->
<!-- French translation : Lucien GENTIS -->
<!-- Reviewed by : Vincent Deffontaines -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ja.xsl"?>
-<!-- English Revision: 1933071:1934122 (outdated) -->
+<!-- English Revision: 1933071:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='EUC-KR' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ko.xsl"?>
-<!-- English Revision: 1933071:1934122 (outdated) -->
+<!-- English Revision: 1933071:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.tr.xsl"?>
-<!-- English Revision: 1933071:1934122 (outdated) -->
+<!-- English Revision: 1933071:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.zh-cn.xsl"?>
-<!-- English Revision: 1933071:1934122 (outdated) -->
+<!-- English Revision: 1933071:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<p>To limit the characters escaped this way, see <a href="#flag_bne">#flag_bne</a>
and <a href="#flag_bctls">#flag_bctls</a></p>
+
+<p>See <a href="tech.html#encoding">URL Encoding and Decoding</a> for
+a full explanation of how Apache decodes URIs before pattern matching.</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="flag_bnp" id="flag_bnp">BNP|backrefnoplus (don't escape space to +)</a> <a title="Permanent link" href="#flag_bnp" class="permalink">¶</a></h2>
<p>This flag is available in version 2.4.26 and later.</p>
+
+<p>See <a href="tech.html#encoding">URL Encoding and Decoding</a> for
+background on how encoding is handled in the rewrite pipeline.</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="flag_bctls" id="flag_bctls">BCTLS</a> <a title="Permanent link" href="#flag_bctls" class="permalink">¶</a></h2>
the next rule, and any other rules that are chained together, are
skipped.</p>
+<pre class="prettyprint lang-config"># Rewrite legacy product URLs to the new catalog app,
+# and add a tracking parameter — but only for the rewritten ones.
+RewriteRule "^/products/([0-9]+)$" "/catalog/item/$1" [C]
+RewriteRule "^/catalog/(.*)$" "/catalog/$1?via=legacy" [QSA]</pre>
+
+
+<p>Without the [C] flag, the second rule would also match requests
+that arrive at <code>/catalog/</code> directly. The chain ensures the
+second rule is only applied when the first rule matched.</p>
+
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="flag_co" id="flag_co">CO|cookie</a> <a title="Permanent link" href="#flag_co" class="permalink">¶</a></h2>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="flag_dpi" id="flag_dpi">DPI|discardpath</a> <a title="Permanent link" href="#flag_dpi" class="permalink">¶</a></h2>
-<p>The DPI flag causes the PATH_INFO portion of the rewritten URI to be
+<p>The DPI flag causes the PATH_INFO portion of the rewritten URL-path to be
discarded.</p>
-<p>In per-directory context, the URI each <code class="directive">RewriteRule</code>
-compares against is the concatenation of the current values of the URI
-and PATH_INFO.</p>
-
-<p>The current URI can be the initial URI as requested by the client, the
-result of a previous round of <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> processing, or the result of
-a prior rule in the current round of <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> processing.</p>
-
-<p>In contrast, the PATH_INFO that is appended to the URI before each
-rule reflects only the value of PATH_INFO before this round of
-<code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> processing. As a consequence, if large portions
-of the URI are matched and copied into a substitution in multiple
+
+<p>In <a class="glossarylink" href="../glossary.html#perdirectory" title="see glossary">per-directory context</a>,
+the URL-path each <code class="directive">RewriteRule</code> compares against
+is the concatenation of the current URL-path and PATH_INFO.</p>
+
+<p>The current URL-path can be the initial path as requested by the
+client, the result of a previous round of <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
+processing, or the result of a prior rule in the current round of
+<code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> processing.</p>
+
+<p>In contrast, the PATH_INFO that is appended to the URL-path before
+each rule reflects only the value of PATH_INFO before this round of
+<code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> processing. As a consequence, if large
+portions of the URL-path are matched and copied into a substitution in multiple
<code class="directive">RewriteRule</code> directives, without regard for
-which parts of the URI came from the current PATH_INFO, the final
-URI may have multiple copies of PATH_INFO appended to it.</p>
+which parts of the URL-path came from the current PATH_INFO, the final
+URL-path may have multiple copies of PATH_INFO appended to it.</p>
<p>Use this flag on any substitution where the PATH_INFO that resulted
from the previous mapping of this request to the filesystem is not of
completes. Subsequent rules during this round of processing will see
only the direct result of substitutions, without any PATH_INFO
appended.</p>
+
+<pre class="prettyprint lang-config"># Request: /app/script.php/extra/path (PATH_INFO is /extra/path)
+# Without DPI, the substitution would see "script.php/extra/path"
+# and could inadvertently copy PATH_INFO into the result.
+RewriteRule "^script\.php(.*)$" "/new-app/handler$1" [DPI]</pre>
+
+
+<p>The [DPI] flag discards <code>/extra/path</code> so that only the
+substitution result (<code>/new-app/handler</code>) is passed to
+subsequent rules or the final request.</p>
+
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="flag_e" id="flag_e">E|env</a> <a title="Permanent link" href="#flag_e" class="permalink">¶</a></h2>
CustomLog directives.</p>
<p>The following example sets an environment variable called 'image' to a
-value of '1' if the requested URI is an image file. Then, that
+value of '1' if the requested URL-path is an image file. Then, that
environment variable is used to exclude those requests from the access
log.</p>
<p>Note that environment variables do not survive an external
redirect. You might consider using the [CO] flag to set a
-cookie. For per-directory rewrites, where the final
-substitution is processed as an internal redirect, environment
-variables from the previous round of rewriting are prefixed with
-"REDIRECT_". </p>
+cookie.</p>
+
+ <div class="note"><h3>REDIRECT_ prefix after internal redirects</h3>
+ <p>In <a class="glossarylink" href="../glossary.html#perdirectory" title="see glossary">per-directory context</a>,
+ a successful substitution triggers an internal redirect. When this
+ happens, all environment variables set during the previous pass —
+ including those created with <code>[E=VAR:VAL]</code> — are renamed
+ with a <code>REDIRECT_</code> prefix. A variable you set as
+ <code>rewritten</code> becomes <code>REDIRECT_rewritten</code> in
+ the redirected request.</p>
+
+ <p>To test for the renamed variable, reference it with the
+ prefix:</p>
+ </div>
+
+<pre class="prettyprint lang-config">RewriteRule "^/horses/(.*)" "/ponies/$1" [E=rewritten:1]
+
+# In the next pass, the variable has been renamed:
+RewriteCond "%{ENV:REDIRECT_rewritten}" =1
+RewriteRule "^/ponies/(.*)" "-" [E=seen_redirect:1,L]</pre>
+
+
+ <p>If the request is redirected multiple times, the prefix stacks:
+ <code>REDIRECT_REDIRECT_rewritten</code>, and so on. See
+ <a href="../env.html#redirect-vars">REDIRECT_ variables</a> for
+ the complete description of this mechanism.</p>
+
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="flag_end" id="flag_end">END</a> <a title="Permanent link" href="#flag_end" class="permalink">¶</a></h2>
<p>This example uses the "-" syntax for the rewrite target, which means
-that the requested URI is not modified. There's no reason to rewrite to
-another URI, if you're going to forbid the request.</p>
+that the requested URL-path is not modified. There's no reason to rewrite to
+another URL-path, if you're going to forbid the request.</p>
<p>When using [F], an [L] is implied - that is, the response is returned
immediately, and no further rules are evaluated.</p>
<pre class="prettyprint lang-config">RewriteRule "(.*)A(.*)" "$1B$2" [N]</pre>
<p>You can think of this as a <code>while</code> loop: While this
-pattern still matches (i.e., while the URI still contains an
+pattern still matches (i.e., while the URL-path still contains an
<code>A</code>), perform this substitution (i.e., replace the
<code>A</code> with a <code>B</code>).</p>
<h2><a name="flag_nc" id="flag_nc">NC|nocase</a> <a title="Permanent link" href="#flag_nc" class="permalink">¶</a></h2>
<p>Use of the [NC] flag causes the <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to be matched in a
case-insensitive manner. That is, it doesn't care whether letters appear
-as upper-case or lower-case in the matched URI.</p>
+as upper-case or lower-case in the matched URL-path.</p>
<p>In the example below, any request for an image file will be proxied
to your dedicated image server. The match is case-insensitive, so that
then result in a 404 Not Found error condition.
</p>
+<p>See <a href="tech.html#encoding">URL Encoding and Decoding</a> for
+the full picture of how Apache encodes and decodes URIs during
+rewriting.</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="flag_ns" id="flag_ns">NS|nosubreq</a> <a title="Permanent link" href="#flag_ns" class="permalink">¶</a></h2>
are not subrequests - the browser requests them as separate HTTP
requests.
</p>
+
+<pre class="prettyprint lang-config"># Only rewrite direct requests to the front controller,
+# not subrequests from SSI includes or mod_dir.
+RewriteCond "%{REQUEST_FILENAME}" !-f
+RewriteCond "%{REQUEST_FILENAME}" !-d
+RewriteRule "^(.*)$" "/app/index.php?page=$1" [NS,L]</pre>
+
+
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="flag_p" id="flag_p">P|proxy</a> <a title="Permanent link" href="#flag_p" class="permalink">¶</a></h2>
considered.</p>
<p>
-You must make sure that the substitution string is a valid URI
+You must make sure that the substitution string is a valid URL
(typically starting with <code>http://</code><em>hostname</em>) which can be
handled by the <code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code>. If not, you will get an
error from the proxy module. Use this flag to achieve a
<p>
The target (or substitution string) in a RewriteRule is assumed to be a
file path, by default. The use of the [PT] flag causes it to be treated
-as a URI instead. That is to say, the
+as a URL-path instead. That is to say, the
use of the [PT] flag causes the result of the <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to be passed back through
URL mapping, so that location-based mappings, such as <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code>, <code class="directive"><a href="../mod/mod_alias.html#redirect">Redirect</a></code>, or <code class="directive"><a href="../mod/mod_alias.html#scriptalias">ScriptAlias</a></code>, for example, might have a
chance to take effect.
<div class="section">
<h2><a name="flag_qsa" id="flag_qsa">QSA|qsappend</a> <a title="Permanent link" href="#flag_qsa" class="permalink">¶</a></h2>
<p>
-When the replacement URI contains a query string, the default behavior
+When the replacement URL contains a query string, the default behavior
of <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> is to discard
the existing query string, and replace it with the newly generated one.
Using the [QSA] flag causes the query strings to be combined.
<div class="section">
<h2><a name="flag_qsd" id="flag_qsd">QSD|qsdiscard</a> <a title="Permanent link" href="#flag_qsd" class="permalink">¶</a></h2>
<p>
-When the requested URI contains a query string, and the target URI does
+When the requested URL contains a query string, and the target URL does
not, the default behavior of <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> is to copy that query
-string to the target URI. Using the [QSD] flag causes the query string
+string to the target URL. Using the [QSD] flag causes the query string
to be discarded.
</p>
</p>
<p>
-If the target URI has a query string, the default behavior will be
+If the target URL has a query string, the default behavior will be
observed - that is, the original query string will be discarded and
replaced with the query string in the <code>RewriteRule</code> target
-URI.
+URL.
</p>
+<pre class="prettyprint lang-config"># Redirect old search URLs to the new path, discarding the query string.
+# /search?q=term&page=2 becomes /find (query string removed)
+RewriteRule "^/search" "/find" [QSD,R=301,L]</pre>
+
+
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="flag_qsl" id="flag_qsl">QSL|qslast</a> <a title="Permanent link" href="#flag_qsl" class="permalink">¶</a></h2>
<p>
You will almost always want to use [R] in conjunction with [L] (that is,
use [R,L]) because on its own, the [R] flag prepends
-<code>http://thishost[:thisport]</code> to the URI, but then passes this
+<code>http://thishost[:thisport]</code> to the URL-path, but then passes this
on to the next rule in the ruleset, which can often result in 'Invalid
URI in request' warnings.
</p>
spec. Using an unrecognized status code will result in a 500 error and
error log message.</p>
+<pre class="prettyprint lang-config"># Redirect requests for the old docs path to the new location.
+RewriteRule "^/docs/(.*)$" "http://docs.example.com/$1" [R=301,L]</pre>
+
+
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="flag_s" id="flag_s">S|skip</a> <a title="Permanent link" href="#flag_s" class="permalink">¶</a></h2>
RewriteCond</a></code> directives match). This can be thought of as a
<code>goto</code> statement in your rewrite ruleset. In the following
example, we only want to run the <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">
-RewriteRule</a></code> if the requested URI doesn't correspond with an
+RewriteRule</a></code> if the requested URL-path doesn't correspond with an
actual file.</p>
<pre class="prettyprint lang-config"># Is the request for a non-existent file?
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.de.xsl"?>
-<!-- English Revision: 1933062:1934122 (outdated) -->
+<!-- English Revision: 1933062:1934234 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.es.xsl"?>
-<!-- English Revision: 1933062:1934122 (outdated) -->
+<!-- English Revision: 1933062:1934234 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.fr.xsl"?>
-<!-- English Revision: 1933062:1934122 (outdated) -->
+<!-- English Revision: 1933062:1934234 (outdated) -->
<!-- French translation : Lucien GENTIS -->
<!-- Reviewed by : Vincent Deffontaines -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ja.xsl"?>
-<!-- English Revision: 1933062:1934122 (outdated) -->
+<!-- English Revision: 1933062:1934234 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='EUC-KR' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ko.xsl"?>
-<!-- English Revision: 1933062:1934122 (outdated) -->
+<!-- English Revision: 1933062:1934234 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.tr.xsl"?>
-<!-- English Revision: 1933062:1934122 (outdated) -->
+<!-- English Revision: 1933062:1934234 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.zh-cn.xsl"?>
-<!-- English Revision: 1933062:1934122 (outdated) -->
+<!-- English Revision: 1933062:1934234 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<li><img alt="" src="../images/down.gif" /> <a href="#loops">The [L] flag and looping</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#rewritemap-restriction">RewriteMap cannot be
declared in .htaccess</a></li>
+<li><img alt="" src="../images/down.gif" /> <a href="#context-restrictions">Which contexts support rewrite rules?</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#inheritance">Rule inheritance with RewriteOptions</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#debugging">Debugging .htaccess rewrite rules</a></li>
</ul><h3>See also</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Module documentation</a></li><li><a href="intro.html">mod_rewrite introduction</a></li><li><a href="remapping.html">Redirection and remapping</a></li><li><a href="vhosts.html">Virtual hosts</a></li><li><a href="rewritemap.html">Using RewriteMap</a></li><li><a href="avoid.html">When not to use mod_rewrite</a></li><li><a href="flags.html">RewriteRule Flags</a></li><li><a href="tech.html">Technical details</a></li><li><a href="../mod/mod_rewrite.html#what_is_matched">What is matched?</a></li><li><a href="#comments_section">Comments</a></li></ul></div>
mechanism is also why rules can loop — see
<a href="#loops">below</a>.</p>
+
+<p>Because the directory prefix (including the trailing
+slash) is stripped before matching, patterns in
+<a class="glossarylink" href="../glossary.html#perdirectory" title="see glossary">per-directory context</a>
+will <em>never</em> match a leading slash. A pattern
+beginning with <code>^/</code> will never match in this
+context.</p>
+
+<p>If you need to match against the full original
+URL-path (including the directory prefix), use
+<code>%{REQUEST_URI}</code> in a
+<code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>:</p>
+
+<pre class="prettyprint lang-config">RewriteCond "%{REQUEST_URI}" "^/admin/"
+RewriteRule "^.*$" "-" [F]</pre>
+
+
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="rewritebase" id="rewritebase">When you need RewriteBase</a> <a title="Permanent link" href="#rewritebase" class="permalink">¶</a></h2>
<code>dbm:</code>, <code>dbd:</code>, and <code>prg:</code> map types)
would be prohibitively expensive to repeat each time.</p>
+</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
+<div class="section">
+<h2><a name="context-restrictions" id="context-restrictions">Which contexts support rewrite rules?</a> <a title="Permanent link" href="#context-restrictions" class="permalink">¶</a></h2>
+
+<p>Rewrite rules are supported in
+<a class="glossarylink" href="../glossary.html#perdirectory" title="see glossary">per-directory context</a>
+(<a href="../howto/htaccess.html">.htaccess</a> files,
+<code class="directive"><a href="../mod/core.html#directory"><Directory></a></code>, and
+<code class="directive"><a href="../mod/core.html#if"><If></a></code> blocks).</p>
+
+<p>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 (including their regex counterparts), this is
+unsupported and should never be necessary. Relative
+substitutions, in particular, are likely to break in
+these contexts.</p>
+
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="inheritance" id="inheritance">Rule inheritance with RewriteOptions</a> <a title="Permanent link" href="#inheritance" class="permalink">¶</a></h2>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.de.xsl"?>
-<!-- English Revision: 1933067:1934108 (outdated) -->
+<!-- English Revision: 1933067:1934228 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.es.xsl"?>
-<!-- English Revision: 1933067:1934108 (outdated) -->
+<!-- English Revision: 1933067:1934228 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.fr.xsl"?>
-<!-- English Revision: 1933067:1934108 (outdated) -->
+<!-- English Revision: 1933067:1934228 (outdated) -->
<!-- French translation : Lucien GENTIS -->
<!-- Reviewed by : Vincent Deffontaines -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ja.xsl"?>
-<!-- English Revision: 1933067:1934108 (outdated) -->
+<!-- English Revision: 1933067:1934228 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='EUC-KR' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ko.xsl"?>
-<!-- English Revision: 1933067:1934108 (outdated) -->
+<!-- English Revision: 1933067:1934228 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.tr.xsl"?>
-<!-- English Revision: 1933067:1934108 (outdated) -->
+<!-- English Revision: 1933067:1934228 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.zh-cn.xsl"?>
-<!-- English Revision: 1933067:1934108 (outdated) -->
+<!-- English Revision: 1933067:1934228 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
This file is generated from xml source: DO NOT EDIT
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-->
-<title>Le module Apache mod_rewrite - Serveur HTTP Apache Version 2.5</title>
+<title>Guide pour le module Apache mod_rewrite - Serveur HTTP Apache Version 2.5</title>
<link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
<link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
<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" />
<img alt="" src="../images/feather.png" /></div>
<div class="up"><a href="../"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
<div id="path">
-<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">Serveur HTTP</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.5</a></div><div id="page-content"><div id="preamble"><h1>Le module Apache mod_rewrite</h1>
+<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">Serveur HTTP</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.5</a></div><div id="page-content"><div id="preamble"><h1>Guide pour le module Apache mod_rewrite</h1>
<div class="toplang">
<p><span>Langues Disponibles: </span><a href="../de/rewrite/" hreflang="de" rel="alternate" title="Deutsch"> de </a> |
<a href="../en/rewrite/" hreflang="en" rel="alternate" title="English"> en </a> |
<a href="../tr/rewrite/" hreflang="tr" rel="alternate" title="Türkçe"> tr </a> |
<a href="../zh-cn/rewrite/" hreflang="zh-cn" rel="alternate" title="Simplified Chinese"> zh-cn </a></p>
</div>
-<div class="outofdate">Cette traduction peut être périmée. Vérifiez la version
- anglaise pour les changements récents.</div>
+<p><em>Le point fort de mod_rewrite réside dans le fait qu’il possède toutes les
+capacités de configuration et la flexibilité de Sendmail ;mais c’est aussi son
+point faible.</em>
+-- Brian Behlendorf</p>
+
+<p><em>Malgré la multitude d'exemples et de documentations, mod_rewrite est de
+la magie noire. De la magie noire très "cool", mais de la magie noire
+quand-même.</em></p>
<p><code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> permet de modifier les requêtes
entrantes dynamiquement, en fonction de règles manipulant des <a href="intro.html#regex">expressions rationnelles</a>. Vous pouvez
ainsi relier des URLs arbitraires à votre propre structure d'URLs
interne comme vous le souhaitez.</p>
- <p>Il fournit un
- mécanisme de manipulation d'URL particulièrement souple et
- puissant en supportant un nombre illimité de règles et de
- conditions attachées à chaque règle. Les manipulations d'URLs
- peuvent dépendre de tests variés : les URLs peuvent
- être finement caractérisées en fonction de variables du serveur,
- de variables d'environnement, d'en-têtes HTTP, de repères
- temporels, de recherches dans des bases de données
- externes, ou même de requêtes vers des bases de données externes
- et de différents gestionnaires ou programmes externes.</p>
+<p>Ce guide est un complément du <a href="../mod/mod_rewrite.html">manuel de
+référence</a> avec des exemples commentés, des explications conceptuelles et des
+conseils pratiques. Il est organisé comme suit :</p>
+
+<dl>
+<dt><a href="intro.html">Introduction</a></dt>
+<dd>Concepts de base : syntaxe des expressions rationnelles, les bases des
+directives RewriteRule et RewriteCond et la manière dont mod_rewrite s’insère
+dans le cycle de vie du traitement des requêtes.</dd>
+
+<dt><a href="htaccess.html">Réécritures en fonction du répertoire</a></dt>
+<dd>Les principales différences entre l’utilisation des règles de réécriture au
+niveau de la configuration globale du serveur et leur utilisation dans un
+contexte de <a class="glossarylink" href="../glossary.html#perdirectory" title="voir glossaire">répertoire</a>, y compris la
+suppression du chemin, RewriteBase et la gestion du bouclage par le drapeau [L].</dd>
+
+<dt><a href="flags.html">Drapeaux des règles de réécriture</a></dt>
+<dd>Une référence complète de tous les drapeaux qui peuvent modifier le
+comportement d’une règle de réécriture avec des exemples pour chacun d’entre
+eux.</dd>
+
+<dt><a href="rewritemap.html">Utiliser RewriteMap</a></dt>
+<dd>Comment utiliser des sources de recherche externes — fichiers texte,
+bases de données DBM, requêtes SQL et fonctions internes — pour piloter
+vos règles de réécriture.</dd>
+
+<dt><a href="remapping.html">Redirection et remappage</a></dt>
+<dd>Des recettes pour les tâches courantes : redirection HTTPS, noms d’hôte
+canoniques, normalisation des barres obliques de fin, routage du contrôleur
+frontal, et plus encore.</dd>
- <p>Les règles de réécriture peuvent agir sur l'ensemble des URLs (la partie chemin
- et la chaîne de paramètres) et peuvent être utilisées dans le contexte du serveur principal
- (<code>httpd.conf</code>), mais aussi dans le contexte des
- serveurs virtuels (sections <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code>), ou dans le
- contexte des
- répertoires (fichiers <code>.htaccess</code> et blocs
- <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code>). Le résultat
- réécrit peut conduire vers d'autres règles à un
- traitement secondaire interne, une redirection vers une requête
- externe ou même l'envoi vers un serveur mandataire, en fonction
- des <a href="flags.html">drapeaux</a> que vous attachez aux
- règles</p>
+<dt><a href="vhosts.html">Serveurs virtuels dynamiques</a></dt>
+<dd>Utilisation de mod_rewrite pour associer dynamiquement les noms d’hôte aux
+racines de document sans devoir créer des blocs VirtualHost individuels.</dd>
- <p><code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> étant très puissant, il peut par
- conséquent être très complexe. Ce document
- complète la <a href="../mod/mod_rewrite.html">documentation de
- référence du module mod_rewrite</a>, et est sensé alléger un
- peu cette complexité, et présenter des exemples largement
- commentés, ainsi que des situations courantes que vous
- pourrez traiter avec <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>. Mais nous voulons aussi vous
- montrer des situations où vous ne devrez pas utiliser
- <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>, et lui préférer d'autres
- fonctionnalités standard d'Apache, évitant ainsi
- d'entrer dans une complexité inutile.</p>
+<dt><a href="avoid.html">Quand ne pas utiliser mod_rewrite</a></dt>
+<dd>De nombreuses tâches courantes sont accomplies de manière bien plus efficace
+avec des directives plus simples. Ce document vous propose des solutions de
+remplacement et vous indique quand il est préférable de les utiliser.</dd>
-<ul>
-<li><a href="../mod/mod_rewrite.html">documentation de
-référence de mod_rewrite</a></li>
-<li><a href="intro.html">Introduction aux expressions rationnelles et à
-mod_rewrite</a></li>
-<li><a href="flags.html">Drapeaux de réécriture</a></li>
-<li><a href="rewritemap.html">Utilisation de RewriteMap</a></li>
-<li><a href="avoid.html">Quand <strong>NE PAS</strong> utiliser mod_rewrite</a></li>
-<li><a href="remapping.html">Utilisation de mod_rewrite pour la
-redirection et la remise en correspondance des URLs</a></li>
-<li><a href="access.html">Utilisation de mod_rewrite pour le
-contrôle d'accès</a></li>
-<li><a href="vhosts.html">Les serveurs virtuels dynamiques avec mod_rewrite</a></li>
-<li><a href="proxy.html">Les serveurs mandataires dynamiques avec mod_rewrite</a></li>
-<li><a href="advanced.html">Techniques avancées</a></li>
-<li><a href="tech.html">Détails techniques</a></li>
-</ul>
+<dt><a href="tech.html">Détails techniques</a></dt>
+<dd>Comment mod_rewrite s’insère dans les phases de traitement des requêtes par
+httpd et l’ordre dans lequel les règles et les conditions sont évaluées.</dd>
+</dl>
</div>
<div id="quickview"><h3>Voir aussi</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Documentation de
référence de mod_rewrite</a></li><li><a href="../urlmapping.html">Mise en correspondance des URLs
<variant outdated="yes">de</variant>
<variant>en</variant>
<variant outdated="yes">es</variant>
- <variant outdated="yes">fr</variant>
+ <variant>fr</variant>
<variant outdated="yes">ja</variant>
<variant outdated="yes">ko</variant>
<variant outdated="yes">tr</variant>
not <code>c/t</code></td></tr>
</table>
-<p>In <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> the <code>!</code> character can be
+<p>The <code>!</code> (Not) character can be
used before a regular expression to negate it. This is, a string will
be considered to have matched only if it does not match the rest of
the expression.</p>
+<p>When using <code>!</code> to negate a pattern, note that
+<a href="#InternalBackRefs">backreferences</a> (e.g.
+<code>$1</code>, <code>$2</code>) are not available, since the
+pattern does not match.</p>
+
+<p>For example, the following will redirect any request that does
+<em>not</em> start with <code>/admin</code></p>
+
+<pre class="prettyprint lang-config">RewriteRule "!^/admin" "/xyz.html" [R,L]</pre>
+
+
<h3><a name="InternalBackRefs" id="InternalBackRefs">Regex Back-Reference Availability</a></h3>
</ol>
<p>The <var>Pattern</var> is a <a href="#regex">regular expression</a>.
-It is initially (for the first rewrite rule or until a substitution occurs)
-matched against the URL-path of the incoming request (the part after the
-hostname but before any question mark indicating the beginning of a query
-string) or, in per-directory context, against the request's path relative
-to the directory for which the rule is defined. Once a substitution has
-occurred, the rules that follow are matched against the substituted
-value.
+In server or virtualhost context, it is matched against the
+%-decoded <a href="../directive-dict.html#Syntax">URL-path</a>
+of the incoming request — the part after the hostname and port,
+and not including the query string (e.g., <code>/app/index.html</code>).
+In <a class="glossarylink" href="../glossary.html#perdirectory" title="see glossary">per-directory context</a>,
+the pattern is matched against the request's path relative to the
+directory for which the rule is defined (with the directory prefix
+stripped — see <a href="htaccess.html#path-stripping">Per-directory
+Rewrites</a> for details).
</p>
+<p>Once a substitution has occurred, any rules that follow are
+matched against the substituted value.</p>
+
+<p>The <var>Pattern</var> is matched only against the URL-path
+— not the hostname, port, or query string. To match against
+those, use a
+<code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> with the
+<code>%{HTTP_HOST}</code>, <code>%{SERVER_PORT}</code>, or
+<code>%{QUERY_STRING}</code> variables respectively.</p>
+
<p class="figure">
<img src="../images/syntax_rewriterule.png" alt="Syntax of the RewriteRule directive" /><br />
<dfn>Figure 2:</dfn> Syntax of the RewriteRule directive.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.de.xsl"?>
-<!-- English Revision: 1933423:1934122 (outdated) -->
+<!-- English Revision: 1933423:1934230 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.es.xsl"?>
-<!-- English Revision: 1933423:1934122 (outdated) -->
+<!-- English Revision: 1933423:1934230 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.fr.xsl"?>
-<!-- English Revision: 1933423:1934122 (outdated) -->
+<!-- English Revision: 1933423:1934230 (outdated) -->
<!-- French translation : Lucien GENTIS -->
<!-- Reviewed by : Vincent Deffontaines -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ja.xsl"?>
-<!-- English Revision: 1933423:1934122 (outdated) -->
+<!-- English Revision: 1933423:1934230 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='EUC-KR' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ko.xsl"?>
-<!-- English Revision: 1933423:1934122 (outdated) -->
+<!-- English Revision: 1933423:1934230 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.tr.xsl"?>
-<!-- English Revision: 1933423:1934122 (outdated) -->
+<!-- English Revision: 1933423:1934230 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.zh-cn.xsl"?>
-<!-- English Revision: 1933423:1934122 (outdated) -->
+<!-- English Revision: 1933423:1934230 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<p>See also the <a href="avoid.html#redirect">When not to use
mod_rewrite</a> document for more discussion of the
<code>Redirect</code> approach.</p>
+
+ <div class="note"><h3>Behind a load balancer or SSL terminator</h3>
+ <p>The <code>%{HTTPS}</code> variable is not a general-purpose
+ environment variable — it queries <code class="module"><a href="../mod/mod_ssl.html">mod_ssl</a></code>
+ directly. If SSL/TLS is terminated at an upstream load balancer
+ or reverse proxy, <code class="module"><a href="../mod/mod_ssl.html">mod_ssl</a></code> is not handling the
+ connection and <code>%{HTTPS}</code> will always report
+ <code>off</code>, even when the original client connected over
+ HTTPS.</p>
+
+ <p>In this situation, check the header set by the upstream proxy
+ instead. Most load balancers set
+ <code>X-Forwarded-Proto</code>:</p>
+ </div>
+
+<pre class="prettyprint lang-config">RewriteEngine On
+RewriteCond "%{HTTP:X-Forwarded-Proto}" =http [NC]
+RewriteRule "^(.*)" "https://%{SERVER_NAME}$1" [R=301,L]</pre>
+
+
+ <div class="warning">
+ <p>Only trust <code>X-Forwarded-Proto</code> if you control the
+ upstream proxy and it overwrites the header on every request. An
+ attacker can forge this header when connecting directly to your
+ server. Consider restricting access so that only your load
+ balancer can reach the backend, or use
+ <code class="module"><a href="../mod/mod_remoteip.html">mod_remoteip</a></code> to validate the source.</p>
+ </div>
+
</dd>
</dl>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.de.xsl"?>
-<!-- English Revision: 1933068:1934122 (outdated) -->
+<!-- English Revision: 1933068:1934200 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.es.xsl"?>
-<!-- English Revision: 1933068:1934122 (outdated) -->
+<!-- English Revision: 1933068:1934200 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.fr.xsl"?>
-<!-- English Revision: 1933068:1934122 (outdated) -->
+<!-- English Revision: 1933068:1934200 (outdated) -->
<!-- French translation : Lucien GENTIS -->
<!-- Reviewed by : Vincent Deffontaines -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ja.xsl"?>
-<!-- English Revision: 1933068:1934122 (outdated) -->
+<!-- English Revision: 1933068:1934200 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='EUC-KR' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ko.xsl"?>
-<!-- English Revision: 1933068:1934122 (outdated) -->
+<!-- English Revision: 1933068:1934200 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.tr.xsl"?>
-<!-- English Revision: 1933068:1934122 (outdated) -->
+<!-- English Revision: 1933068:1934200 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.zh-cn.xsl"?>
-<!-- English Revision: 1933068:1934122 (outdated) -->
+<!-- English Revision: 1933068:1934200 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
the int function, and then use that in your <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>:
</p>
- <p> <strong>Redirect a URI to an all-lowercase version of itself</strong></p>
+ <p> <strong>Redirect a URL-path to an all-lowercase version of itself</strong></p>
<pre class="prettyprint lang-config">RewriteMap lc int:tolower
RewriteRule "(.*)" "${lc:$1}" [R]</pre>
<code class="directive"><a href="../mod/core.html#mutex">Mutex</a></code> directive.</p>
<p>A simple example is shown here which will replace all dashes with
- underscores in a request URI.</p>
+ underscores in a request URL-path.</p>
<p><strong>Rewrite configuration</strong></p>
<pre class="prettyprint lang-config">RewriteMap d2u "prg:/www/bin/dash2under.py" apache:apache
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.de.xsl"?>
-<!-- English Revision: 1932819:1934122 (outdated) -->
+<!-- English Revision: 1932819:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.es.xsl"?>
-<!-- English Revision: 1932819:1934122 (outdated) -->
+<!-- English Revision: 1932819:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.fr.xsl"?>
-<!-- English Revision: 1932819:1934122 (outdated) -->
+<!-- English Revision: 1932819:1934233 (outdated) -->
<!-- French translation : Lucien GENTIS -->
<!-- Reviewed by : Vincent Deffontaines -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ja.xsl"?>
-<!-- English Revision: 1932819:1934122 (outdated) -->
+<!-- English Revision: 1932819:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='EUC-KR' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ko.xsl"?>
-<!-- English Revision: 1932819:1934122 (outdated) -->
+<!-- English Revision: 1932819:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.tr.xsl"?>
-<!-- English Revision: 1932819:1934122 (outdated) -->
+<!-- English Revision: 1932819:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.zh-cn.xsl"?>
-<!-- English Revision: 1932819:1934122 (outdated) -->
+<!-- English Revision: 1932819:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
and URL matching.</p>
</div>
<div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#InternalAPI">API Phases</a></li>
+<li><img alt="" src="../images/down.gif" /> <a href="#order">Module Processing Order</a></li>
+<li><img alt="" src="../images/down.gif" /> <a href="#encoding">URL Encoding and Decoding</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#InternalRuleset">Ruleset Processing</a></li>
</ul><h3>See also</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Module documentation</a></li><li><a href="intro.html">mod_rewrite introduction</a></li><li><a href="remapping.html">Redirection and remapping</a></li><li><a href="flags.html">RewriteRule Flags</a></li><li><a href="vhosts.html">Virtual hosts</a></li><li><a href="rewritemap.html">Using RewriteMap</a></li><li><a href="htaccess.html">Per-directory Rewrites</a></li><li><a href="avoid.html">When not to use mod_rewrite</a></li><li><a href="#comments_section">Comments</a></li></ul></div>
<div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
document for practical details on path stripping, RewriteBase, and
how to avoid looping.</p>
+</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
+<div class="section">
+<h2><a name="order" id="order">Module Processing Order</a> <a title="Permanent link" href="#order" class="permalink">¶</a></h2>
+
+ <p><code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> and <code class="module"><a href="../mod/mod_alias.html">mod_alias</a></code> both
+ operate during the URL-to-filename translation phase, but
+ <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> runs <strong>first</strong> regardless
+ of the order in which directives appear in the configuration file.
+ This is determined by the hook priority each module registers, not
+ by source order.</p>
+
+ <p>The practical consequence: when both <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> and <code class="directive"><a href="../mod/mod_alias.html#redirect">Redirect</a></code> (or <code class="directive"><a href="../mod/mod_alias.html#redirectmatch">RedirectMatch</a></code>) are present in the
+ same server or virtual-host context, the rewrite rules are
+ evaluated first. If a <code>RewriteRule</code> matches and rewrites
+ the URL-path (or returns a redirect), <code>Redirect</code> never sees
+ the request.</p>
+
+ <pre class="prettyprint lang-config"># In this configuration, the Redirect is never reached for /old
+# because the RewriteRule matches first — even though
+# the Redirect appears earlier in the file.
+Redirect "/old" "http://example.com/new"
+RewriteRule "^/old" "/other" [L]</pre>
+
+
+ <div class="note"><h3>Per-directory context reverses the order</h3>
+ <p>In <a class="glossarylink" href="../glossary.html#perdirectory" title="see glossary">per-directory context</a>,
+ the situation is different. <code class="module"><a href="../mod/mod_alias.html">mod_alias</a></code> directives like
+ <code>Redirect</code> still run in the URL-to-filename translation
+ phase, but <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> rules run later, in the
+ Fixup phase. This means that in per-directory context,
+ <code>Redirect</code> is evaluated <em>before</em>
+ <code>RewriteRule</code>.</p>
+ </div>
+
+ <p>Because of this inconsistency between contexts, mixing
+ <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> and <code class="module"><a href="../mod/mod_alias.html">mod_alias</a></code>
+ directives in the same scope is a common source of confusion. The
+ simplest advice: choose one module for a given task. If you need
+ rewrite conditions or pattern matching, use
+ <code>RewriteRule</code> exclusively. If a simple prefix redirect
+ suffices, use <code>Redirect</code> and don't add rewrite rules
+ that might interact with it.</p>
+
+</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
+<div class="section">
+<h2><a name="encoding" id="encoding">URL Encoding and Decoding</a> <a title="Permanent link" href="#encoding" class="permalink">¶</a></h2>
+
+ <p>Apache httpd unescapes URL-encoded characters in the request URL-path before any
+ <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> pattern
+ matching takes place. A request for
+ <code>/my%20page/cats%3Fdogs</code> is decoded to
+ <code>/my page/cats?dogs</code>, and that decoded string is what the
+ <code>RewriteRule</code> pattern matches against.</p>
+
+ <p>This means you cannot write a pattern that matches the literal
+ URL-encoded form. If you need to distinguish
+ <code>/horses%2Fponies</code> from <code>/horses/ponies</code>, use
+ <code>%{THE_REQUEST}</code> in a <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> — it preserves the
+ original request line exactly as the client sent it, before any
+ decoding:</p>
+
+<pre class="prettyprint lang-config"># Match only the literally-encoded %2F, not a real path separator
+RewriteCond "%{THE_REQUEST}" "/horses%2F"
+RewriteRule "^/horses/ponies$" "/special-handler" [L]</pre>
+
+
+ <p>After substitution, <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> re-encodes the
+ resulting URL-path for output. Several flags control this behavior:</p>
+
+ <ul>
+ <li><a href="flags.html#flag_b">[B]</a> — re-escape
+ backreferences so that special characters captured from the
+ decoded URL-path are not interpreted as delimiters in the
+ substitution.</li>
+
+ <li><a href="flags.html#flag_bnp">[BNP]</a> — when [B] is
+ active, encode spaces as <code>%20</code> rather than
+ <code>+</code> (appropriate for path components, not query
+ strings).</li>
+
+ <li><a href="flags.html#flag_ne">[NE]</a> — suppress the
+ default escaping of special characters in the substitution
+ result, allowing literal <code>#</code>, <code>?</code>, and
+ other characters to pass through unmodified on external
+ redirects.</li>
+ </ul>
+
+ <h3><a name="allowencodedslashes" id="allowencodedslashes">AllowEncodedSlashes</a></h3>
+
+
+ <p>By default, Apache returns 404 for any URL containing an encoded
+ slash (<code>%2F</code>). The <code class="directive"><a href="../mod/core.html#allowencodedslashes">AllowEncodedSlashes</a></code> directive controls
+ this behavior:</p>
+
+ <ul>
+ <li><code>Off</code> (default) — reject <code>%2F</code> with
+ 404.</li>
+ <li><code>On</code> — allow <code>%2F</code> and decode it to
+ <code>/</code> before passing to handlers.</li>
+ <li><code>NoDecode</code> — allow <code>%2F</code> but keep it
+ in encoded form, letting the backend application distinguish it
+ from a real path separator.</li>
+ </ul>
+
+ <p>When using the <a href="flags.html#flag_b">[B]</a> flag with
+ URLs that may contain encoded slashes, you typically need
+ <code>AllowEncodedSlashes NoDecode</code> to prevent Apache from
+ rejecting the re-encoded result.</p>
+
+
+
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="InternalRuleset" id="InternalRuleset">Ruleset Processing</a> <a title="Permanent link" href="#InternalRuleset" class="permalink">¶</a></h2>
<div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#InternalAPI">Phases de l'API</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#InternalRuleset">Traitement du jeu de règles</a></li>
</ul><h3>Voir aussi</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Documentation du module mod_rewrite</a></li><li><a href="intro.html">Introduction à mod_rewrite</a></li><li><a href="remapping.html">Redirection et remise en
-correspondance</a></li><li><a href="access.html">Contrôle d'accès</a></li><li><a href="vhosts.html">Serveurs virtuels</a></li><li><a href="proxy.html">Mise en cache</a></li><li><a href="rewritemap.html">Utilisation de RewriteMap</a></li><li><a href="advanced.html">Techniques avancées</a></li><li><a href="avoid.html">Quand ne pas utiliser mod_rewrite</a></li><li><a href="#comments_section">Commentaires</a></li></ul></div>
+correspondance</a></li><li><a href="flags.html">Drapeaux de réécriture</a></li><li><a href="vhosts.html">Serveurs virtuels</a></li><li><a href="rewritemap.html">Utilisation de RewriteMap</a></li><li><a href="htaccess.html">Réécritures en fonction du répertoire</a></li><li><a href="avoid.html">Quand ne pas utiliser mod_rewrite</a></li><li><a href="#comments_section">Commentaires</a></li></ul></div>
<div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="InternalAPI" id="InternalAPI">Phases de l'API</a> <a title="Lien permanent" href="#InternalAPI" class="permalink">¶</a></h2>
<code>REQUEST_URI</code> soit vers une nouvelle URL, soit vers un
nom de fichier.</p>
- <p>Dans un contexte de niveau répertoire (autrement dit dans les
- fichiers <code>.htaccess</code> et les sections
- <code>Directory</code>), les règles de réécriture s'appliquent après
- la traduction de l'URL en nom de fichier. C'est pourquoi le chemin
- URL auquel <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> compare initialement les directives
- <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> est le
- chemin complet vers le nom de fichier traduit amputé de la partie
- répertoires (y compris le dernier slash).</p>
-
- <p>Un exemple : si les règles se trouvent dans
- /var/www/foo/.htaccess et si une requête pour /foo/bar/baz est
- traité, une expression comme ^bar/baz$ correspondra.</p>
-
- <p>Si une substitution intervient dans un contexte de répertoire,
- une nouvelle sous-requête interne est générée avec la nouvelle URL,
- ce qui relance le traitement des phases de la requête. Si la
- substitution est un chemin relatif, la directive <code class="directive"><a href="../mod/mod_rewrite.html#rewritebase">RewriteBase</a></code> détermine le chemin URL
- devant préfixer cette substitution. Dans un contexte de répertoire,
- il faut s'assurer de créer des règles qui
- n'effectueront pas de substitution au
- cours d'une passe ultérieure du processus de réécriture au niveau
- répertoire afin d'éviter les bouclages . Voir <a href="https://cwiki.apache.org/confluence/display/httpd/RewriteLooping">Bouclage dans le
- processus de réécriture</a> pour une discussion plus détaillée à
- propos de ce problème.</p>
-
- <p>En conséquence de cette manipulation de l'URL , vous devrez
- pensez à confectionner différemment vos règles de réécriture dans un
- contexte de niveau répertoire. En particulier, rappelez-vous que le
- chemin de répertoire sera absent de l'URL que vos règles de
- réécriture verront. Voici quelques exemples qui permettront de
- clarifier les choses :</p>
-
- <table class="bordered">
-
- <tr>
- <th>Position de la règle</th>
- <th>Règle</th>
- </tr>
-
- <tr>
- <td>Section VirtualHost</td>
- <td>RewriteRule "^/images/(.+)\.jpg" "/images/$1.gif"</td>
- </tr>
-
- <tr>
- <td>Fichier .htaccess à la racine des documents</td>
- <td>RewriteRule "^images/(.+)\.jpg" "images/$1.gif"</td>
- </tr>
-
- <tr>
- <td>Fichier .htaccess dans le répertoire images</td>
- <td>RewriteRule "^(.+)\.jpg" "$1.gif"</td>
- </tr>
-
- </table>
-
- <p>Pour une étude plus approfondie de la manière dont <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
- manipule les URLs dans les différents contextes, vous pouvez
- consulter les <a href="../mod/mod_rewrite.html#logging">entrées du
- journal</a> générées au cours du processus de réécriture.</p>
+ <p>Dans un <a class="glossarylink" href="../glossary.html#perdirectory" title="voir glossaire">contexte de répertoire</a>,
+ les règles sont appliquées durant la phase "Fixup" après que l’URL a été
+ traduite en nom de fichier. Cela modifie ce à quoi correspond le motif et la
+ manière dont les substitutions sont gérées. Voir le document <a href="htaccess.html#path-stripping">Réécritures en fonction du
+ répertoire</a> pour des détails pratiques à propos de la suppression du
+ chemin, de RewriteBase et de la manière d’éviter un bouclage.</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.de.xsl"?>
-<!-- English Revision: 1933438:1934122 (outdated) -->
+<!-- English Revision: 1933438:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.es.xsl"?>
-<!-- English Revision: 1933438:1934122 (outdated) -->
+<!-- English Revision: 1933438:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.fr.xsl"?>
-<!-- English Revision: 1934122 -->
+<!-- English Revision: 1934122:1934233 (outdated) -->
<!-- French translation : Lucien GENTIS -->
<!-- Reviewed by : Vincent Deffontaines -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ja.xsl"?>
-<!-- English Revision: 1933438:1934122 (outdated) -->
+<!-- English Revision: 1933438:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='EUC-KR' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.ko.xsl"?>
-<!-- English Revision: 1933438:1934122 (outdated) -->
+<!-- English Revision: 1933438:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.tr.xsl"?>
-<!-- English Revision: 1933438:1934122 (outdated) -->
+<!-- English Revision: 1933438:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.zh-cn.xsl"?>
-<!-- English Revision: 1933438:1934122 (outdated) -->
+<!-- English Revision: 1933438:1934233 (outdated) -->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more