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 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5 This file is generated from xml source: DO NOT EDIT
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
8 <title>Apache mod_rewrite Technical Details - Apache HTTP Server</title>
9 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11 <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
12 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
13 <body id="manual-page"><div id="page-header">
14 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
15 <p class="apache">Apache HTTP Server Version 2.0</p>
16 <img alt="" src="../images/feather.gif" /></div>
17 <div class="up"><a href="./index.html"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
19 <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.0</a></div><div id="page-content"><div id="preamble"><h1>Apache mod_rewrite Technical Details</h1>
21 <p><span>Available Languages: </span><a href="../en/rewrite/rewrite_tech.html" title="English"> en </a></p>
24 <p>This document discusses some of the technical details of mod_rewrite
27 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#Internal">Internal Processing</a></li>
28 <li><img alt="" src="../images/down.gif" /> <a href="#InternalAPI">API Phases</a></li>
29 <li><img alt="" src="../images/down.gif" /> <a href="#InternalRuleset">Ruleset Processing</a></li>
30 </ul><h3>See also</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Module
31 documentation</a></li><li><a href="rewrite_intro.html">mod_rewrite
32 introduction</a></li><li><a href="rewrite_guide.html">Practical solutions to common
33 problems</a></li></ul></div>
34 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
36 <h2><a name="Internal" id="Internal">Internal Processing</a></h2>
38 <p>The internal processing of this module is very complex but
39 needs to be explained once even to the average user to avoid
40 common mistakes and to let you exploit its full
42 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
44 <h2><a name="InternalAPI" id="InternalAPI">API Phases</a></h2>
46 <p>First you have to understand that when Apache processes a
47 HTTP request it does this in phases. A hook for each of these
48 phases is provided by the Apache API. Mod_rewrite uses two of
49 these hooks: the URL-to-filename translation hook which is
50 used after the HTTP request has been read but before any
51 authorization starts and the Fixup hook which is triggered
52 after the authorization phases and after the per-directory
53 config files (<code>.htaccess</code>) have been read, but
54 before the content handler is activated.</p>
56 <p>So, after a request comes in and Apache has determined the
57 corresponding server (or virtual server) the rewriting engine
58 starts processing of all mod_rewrite directives from the
59 per-server configuration in the URL-to-filename phase. A few
60 steps later when the final data directories are found, the
61 per-directory configuration directives of mod_rewrite are
62 triggered in the Fixup phase. In both situations mod_rewrite
63 rewrites URLs either to new URLs or to filenames, although
64 there is no obvious distinction between them. This is a usage
65 of the API which was not intended to be this way when the API
66 was designed, but as of Apache 1.x this is the only way
67 mod_rewrite can operate. To make this point more clear
68 remember the following two points:</p>
71 <li>Although mod_rewrite rewrites URLs to URLs, URLs to
72 filenames and even filenames to filenames, the API
73 currently provides only a URL-to-filename hook. In Apache
74 2.0 the two missing hooks will be added to make the
75 processing more clear. But this point has no drawbacks for
76 the user, it is just a fact which should be remembered:
77 Apache does more in the URL-to-filename hook than the API
81 Unbelievably mod_rewrite provides URL manipulations in
82 per-directory context, <em>i.e.</em>, within
83 <code>.htaccess</code> files, although these are reached
84 a very long time after the URLs have been translated to
85 filenames. It has to be this way because
86 <code>.htaccess</code> files live in the filesystem, so
87 processing has already reached this stage. In other
88 words: According to the API phases at this time it is too
89 late for any URL manipulations. To overcome this chicken
90 and egg problem mod_rewrite uses a trick: When you
91 manipulate a URL/filename in per-directory context
92 mod_rewrite first rewrites the filename back to its
93 corresponding URL (which is usually impossible, but see
94 the <code>RewriteBase</code> directive below for the
95 trick to achieve this) and then initiates a new internal
96 sub-request with the new URL. This restarts processing of
99 <p>Again mod_rewrite tries hard to make this complicated
100 step totally transparent to the user, but you should
101 remember here: While URL manipulations in per-server
102 context are really fast and efficient, per-directory
103 rewrites are slow and inefficient due to this chicken and
104 egg problem. But on the other hand this is the only way
105 mod_rewrite can provide (locally restricted) URL
106 manipulations to the average user.</p>
110 <p>Don't forget these two points!</p>
111 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
112 <div class="section">
113 <h2><a name="InternalRuleset" id="InternalRuleset">Ruleset Processing</a></h2>
115 <p>Now when mod_rewrite is triggered in these two API phases, it
116 reads the configured rulesets from its configuration
117 structure (which itself was either created on startup for
118 per-server context or during the directory walk of the Apache
119 kernel for per-directory context). Then the URL rewriting
120 engine is started with the contained ruleset (one or more
121 rules together with their conditions). The operation of the
122 URL rewriting engine itself is exactly the same for both
123 configuration contexts. Only the final result processing is
126 <p>The order of rules in the ruleset is important because the
127 rewriting engine processes them in a special (and not very
128 obvious) order. The rule is this: The rewriting engine loops
129 through the ruleset rule by rule (<code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> directives) and
130 when a particular rule matches it optionally loops through
131 existing corresponding conditions (<code>RewriteCond</code>
132 directives). For historical reasons the conditions are given
133 first, and so the control flow is a little bit long-winded. See
134 Figure 1 for more details.</p>
136 <img src="../images/mod_rewrite_fig1.gif" width="428" height="385" alt="[Needs graphics capability to display]" /><br />
137 <dfn>Figure 1:</dfn>The control flow through the rewriting ruleset
139 <p>As you can see, first the URL is matched against the
140 <em>Pattern</em> of each rule. When it fails mod_rewrite
141 immediately stops processing this rule and continues with the
142 next rule. If the <em>Pattern</em> matches, mod_rewrite looks
143 for corresponding rule conditions. If none are present, it
144 just substitutes the URL with a new value which is
145 constructed from the string <em>Substitution</em> and goes on
146 with its rule-looping. But if conditions exist, it starts an
147 inner loop for processing them in the order that they are
148 listed. For conditions the logic is different: we don't match
149 a pattern against the current URL. Instead we first create a
150 string <em>TestString</em> by expanding variables,
151 back-references, map lookups, <em>etc.</em> and then we try
152 to match <em>CondPattern</em> against it. If the pattern
153 doesn't match, the complete set of conditions and the
154 corresponding rule fails. If the pattern matches, then the
155 next condition is processed until no more conditions are
156 available. If all conditions match, processing is continued
157 with the substitution of the URL with
158 <em>Substitution</em>.</p>
161 <div class="bottomlang">
162 <p><span>Available Languages: </span><a href="../en/rewrite/rewrite_tech.html" title="English"> en </a></p>
163 </div><div id="footer">
164 <p class="apache">Copyright 2009 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>
165 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div>