github.com/krum110487/go-htaccess@v0.0.0-20240316004156-60641c8e7598/tests/data/apache_2_2_34/manual/rewrite/tech.html.en (about) 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" /> 5 <!-- 6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 7 This file is generated from xml source: DO NOT EDIT 8 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 9 --> 10 <title>Apache mod_rewrite Technical Details - Apache HTTP Server Version 2.2</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"> 15 </script> 16 17 <link href="../images/favicon.ico" rel="shortcut icon" /><link href="http://httpd.apache.org/docs/current/rewrite/tech.html" rel="canonical" /></head> 18 <body id="manual-page"><div id="page-header"> 19 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.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> 20 <p class="apache">Apache HTTP Server Version 2.2</p> 21 <img alt="" src="../images/feather.gif" /></div> 22 <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div> 23 <div id="path"> 24 <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.2</a> > <a href="./">Rewrite</a></div><div id="page-content"><div class="retired"><h4>Please note</h4> 25 <p> This document refers to a legacy release (<strong>2.2</strong>) of Apache httpd. The active release (<strong>2.4</strong>) is documented <a href="http://httpd.apache.org/docs/current">here</a>. If you have not already upgraded, please follow <a href="http://httpd.apache.org/docs/current/upgrading.html">this link</a> for more information.</p> 26 <p>You may follow <a href="http://httpd.apache.org/docs/current/rewrite/tech.html">this link</a> to go to the current version of this document.</p></div><div id="preamble"><h1>Apache mod_rewrite Technical Details</h1> 27 <div class="toplang"> 28 <p><span>Available Languages: </span><a href="../en/rewrite/tech.html" title="English"> en </a> | 29 <a href="../fr/rewrite/tech.html" hreflang="fr" rel="alternate" title="Français"> fr </a></p> 30 </div> 31 32 <p>This document discusses some of the technical details of mod_rewrite 33 and URL matching.</p> 34 </div> 35 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#Internal">Internal Processing</a></li> 36 <li><img alt="" src="../images/down.gif" /> <a href="#InternalAPI">API Phases</a></li> 37 <li><img alt="" src="../images/down.gif" /> <a href="#InternalRuleset">Ruleset Processing</a></li> 38 </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="access.html">Controlling access</a></li><li><a href="vhosts.html">Virtual hosts</a></li><li><a href="proxy.html">Proxying</a></li><li><a href="rewritemap.html">Using RewriteMap</a></li><li><a href="advanced.html">Advanced techniques and tricks</a></li><li><a href="avoid.html">When not to use mod_rewrite</a></li></ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div> 39 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> 40 <div class="section"> 41 <h2><a name="Internal" id="Internal">Internal Processing</a></h2> 42 43 <p>The internal processing of this module is very complex but 44 needs to be explained once even to the average user to avoid 45 common mistakes and to let you exploit its full 46 functionality.</p> 47 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> 48 <div class="section"> 49 <h2><a name="InternalAPI" id="InternalAPI">API Phases</a></h2> 50 51 <p>First you have to understand that when Apache processes a 52 HTTP request it does this in phases. A hook for each of these 53 phases is provided by the Apache API. Mod_rewrite uses two of 54 these hooks: the URL-to-filename translation hook which is 55 used after the HTTP request has been read but before any 56 authorization starts and the Fixup hook which is triggered 57 after the authorization phases and after the per-directory 58 config files (<code>.htaccess</code>) have been read, but 59 before the content handler is activated.</p> 60 61 <p>So, after a request comes in and Apache has determined the 62 corresponding server (or virtual server) the rewriting engine 63 starts processing of all mod_rewrite directives from the 64 per-server configuration in the URL-to-filename phase. A few 65 steps later when the final data directories are found, the 66 per-directory configuration directives of mod_rewrite are 67 triggered in the Fixup phase. In both situations mod_rewrite 68 rewrites URLs either to new URLs or to filenames, although 69 there is no obvious distinction between them. This is a usage 70 of the API which was not intended to be this way when the API 71 was designed, but as of Apache 1.x this is the only way 72 mod_rewrite can operate. To make this point more clear 73 remember the following two points:</p> 74 75 <ol> 76 <li>Although mod_rewrite rewrites URLs to URLs, URLs to 77 filenames and even filenames to filenames, the API 78 currently provides only a URL-to-filename hook. In Apache 79 2.0 the two missing hooks will be added to make the 80 processing more clear. But this point has no drawbacks for 81 the user, it is just a fact which should be remembered: 82 Apache does more in the URL-to-filename hook than the API 83 intends for it.</li> 84 85 <li> 86 Unbelievably mod_rewrite provides URL manipulations in 87 per-directory context, <em>i.e.</em>, within 88 <code>.htaccess</code> files, although these are reached 89 a very long time after the URLs have been translated to 90 filenames. It has to be this way because 91 <code>.htaccess</code> files live in the filesystem, so 92 processing has already reached this stage. In other 93 words: According to the API phases at this time it is too 94 late for any URL manipulations. To overcome this chicken 95 and egg problem mod_rewrite uses a trick: When you 96 manipulate a URL/filename in per-directory context 97 mod_rewrite first rewrites the filename back to its 98 corresponding URL (which is usually impossible, but see 99 the <code>RewriteBase</code> directive below for the 100 trick to achieve this) and then initiates a new internal 101 sub-request with the new URL. This restarts processing of 102 the API phases. 103 104 <p>Again mod_rewrite tries hard to make this complicated 105 step totally transparent to the user, but you should 106 remember here: While URL manipulations in per-server 107 context are really fast and efficient, per-directory 108 rewrites are slow and inefficient due to this chicken and 109 egg problem. But on the other hand this is the only way 110 mod_rewrite can provide (locally restricted) URL 111 manipulations to the average user.</p> 112 </li> 113 </ol> 114 115 <p>Don't forget these two points!</p> 116 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> 117 <div class="section"> 118 <h2><a name="InternalRuleset" id="InternalRuleset">Ruleset Processing</a></h2> 119 120 <p>Now when mod_rewrite is triggered in these two API phases, it 121 reads the configured rulesets from its configuration 122 structure (which itself was either created on startup for 123 per-server context or during the directory walk of the Apache 124 kernel for per-directory context). Then the URL rewriting 125 engine is started with the contained ruleset (one or more 126 rules together with their conditions). The operation of the 127 URL rewriting engine itself is exactly the same for both 128 configuration contexts. Only the final result processing is 129 different. </p> 130 131 <p>The order of rules in the ruleset is important because the 132 rewriting engine processes them in a special (and not very 133 obvious) order. The rule is this: The rewriting engine loops 134 through the ruleset rule by rule (<code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> directives) and 135 when a particular rule matches it optionally loops through 136 existing corresponding conditions (<code>RewriteCond</code> 137 directives). For historical reasons the conditions are given 138 first, and so the control flow is a little bit long-winded. See 139 Figure 1 for more details.</p> 140 <p class="figure"> 141 <img src="../images/rewrite_rule_flow.png" alt="Flow of RewriteRule and RewriteCond matching" /><br /> 142 <dfn>Figure 1:</dfn>The control flow through the rewriting ruleset 143 </p> 144 <p>As you can see, first the URL is matched against the 145 <em>Pattern</em> of each rule. When it fails mod_rewrite 146 immediately stops processing this rule and continues with the 147 next rule. If the <em>Pattern</em> matches, mod_rewrite looks 148 for corresponding rule conditions. If none are present, it 149 just substitutes the URL with a new value which is 150 constructed from the string <em>Substitution</em> and goes on 151 with its rule-looping. But if conditions exist, it starts an 152 inner loop for processing them in the order that they are 153 listed. For conditions the logic is different: we don't match 154 a pattern against the current URL. Instead we first create a 155 string <em>TestString</em> by expanding variables, 156 back-references, map lookups, <em>etc.</em> and then we try 157 to match <em>CondPattern</em> against it. If the pattern 158 doesn't match, the complete set of conditions and the 159 corresponding rule fails. If the pattern matches, then the 160 next condition is processed until no more conditions are 161 available. If all conditions match, processing is continued 162 with the substitution of the URL with 163 <em>Substitution</em>.</p> 164 165 </div></div> 166 <div class="bottomlang"> 167 <p><span>Available Languages: </span><a href="../en/rewrite/tech.html" title="English"> en </a> | 168 <a href="../fr/rewrite/tech.html" hreflang="fr" rel="alternate" title="Français"> fr </a></p> 169 </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> 170 <script type="text/javascript"><!--//--><![CDATA[//><!-- 171 var comments_shortname = 'httpd'; 172 var comments_identifier = 'http://httpd.apache.org/docs/2.2/rewrite/tech.html'; 173 (function(w, d) { 174 if (w.location.hostname.toLowerCase() == "httpd.apache.org") { 175 d.write('<div id="comments_thread"><\/div>'); 176 var s = d.createElement('script'); 177 s.type = 'text/javascript'; 178 s.async = true; 179 s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier; 180 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s); 181 } 182 else { 183 d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>'); 184 } 185 })(window, document); 186 //--><!]]></script></div><div id="footer"> 187 <p class="apache">Copyright 2017 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> 188 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.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[//><!-- 189 if (typeof(prettyPrint) !== 'undefined') { 190 prettyPrint(); 191 } 192 //--><!]]></script> 193 </body></html>