tech.html.en revision 35b24427e6d85411ee9551d07a77cf3bb9af18a0
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
5123f066275e7d29f5779cb6c9303659184ec0acwrowe XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5123f066275e7d29f5779cb6c9303659184ec0acwrowe This file is generated from xml source: DO NOT EDIT
5123f066275e7d29f5779cb6c9303659184ec0acwrowe XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<title>Apache mod_rewrite Technical Details - Apache HTTP Server</title>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<link href="/style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<link href="/images/favicon.ico" rel="shortcut icon" /></head>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<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>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<div class="up"><a href="./"><img title="<-" alt="<-" src="/images/left.gif" /></a></div>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<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.3</a> > <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Apache mod_rewrite Technical Details</h1>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<p><span>Available Languages: </span><a href="/en/rewrite/tech.html" title="English"> en </a> |
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<a href="/fr/rewrite/tech.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a></p>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<p>This document discusses some of the technical details of mod_rewrite
5123f066275e7d29f5779cb6c9303659184ec0acwroweand URL matching.</p>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#InternalAPI">API Phases</a></li>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<li><img alt="" src="/images/down.gif" /> <a href="#InternalRuleset">Ruleset Processing</a></li>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe</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</a></li><li><a href="avoid.html">When not to use mod_rewrite</a></li></ul></div>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe<h2><a name="InternalAPI" id="InternalAPI">API Phases</a></h2>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe <p>The Apache HTTP Server handles requests in several phases. At
5123f066275e7d29f5779cb6c9303659184ec0acwrowe each of these phases, one or more modules may be called upon to
5123f066275e7d29f5779cb6c9303659184ec0acwrowe handle that portion of the request lifecycle. Phases include things
5123f066275e7d29f5779cb6c9303659184ec0acwrowe like URL-to-filename translation, authentication, authorization,
5123f066275e7d29f5779cb6c9303659184ec0acwrowe content, and logging. (This is not an exhaustive list.)</p>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe <p>mod_rewrite acts in two of these phases (or "hooks", as they are
5123f066275e7d29f5779cb6c9303659184ec0acwrowe often called) to influence how URLs may be rewritten.</p>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe <p>First, it uses the URL-to-filename translation hook, which occurs
5123f066275e7d29f5779cb6c9303659184ec0acwrowe after the HTTP request has been read, but before any authorization
5123f066275e7d29f5779cb6c9303659184ec0acwrowe starts. Secondly, it uses the Fixup hook, which is after the
5123f066275e7d29f5779cb6c9303659184ec0acwrowe authorization phases, and after per-directory configuration files
5123f066275e7d29f5779cb6c9303659184ec0acwrowe (<code>.htaccess</code> files) have been read, but before the
5123f066275e7d29f5779cb6c9303659184ec0acwrowe content handler is called.</p>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe <p>So, after a request comes in and a corresponding server or
5123f066275e7d29f5779cb6c9303659184ec0acwrowe virtual host has been determined, the rewriting engine starts
5123f066275e7d29f5779cb6c9303659184ec0acwrowe processing any <code>mod_rewrite</code> directives appearing in the
5123f066275e7d29f5779cb6c9303659184ec0acwrowe per-server configuration. (i.e., in the main server configuration file
5ac28f3fe2417368757f29cf381338357605fd52wrowe and <code class="directive"><a href="/mod/core.html#virtualhost"><Virtualhost></a></code>
5ac28f3fe2417368757f29cf381338357605fd52wrowe sections.) This happens in the URL-to-filename phase.</p>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe <p>A few steps later, once the final data directories have been found,
5123f066275e7d29f5779cb6c9303659184ec0acwrowe the per-directory configuration directives (<code>.htaccess</code>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe files and <code class="directive"><a href="/mod/core.html#directory"><Directory></a></code> blocks) are applied. This
5123f066275e7d29f5779cb6c9303659184ec0acwrowe happens in the Fixup phase.</p>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe <p>In each of these cases, mod_rewrite rewrites the
5123f066275e7d29f5779cb6c9303659184ec0acwrowe <code>REQUEST_URI</code> either to a new URL, or to a filename.</p>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe <p>In per-directory context (i.e., within <code>.htaccess</code> files
5123f066275e7d29f5779cb6c9303659184ec0acwrowe and <code>Directory</code> blocks), these rules are being applied
5123f066275e7d29f5779cb6c9303659184ec0acwrowe after a URL has already been translated to a filename. Because of
5123f066275e7d29f5779cb6c9303659184ec0acwrowe this, mod_rewrite temporarily translates the filename back into a URL,
5123f066275e7d29f5779cb6c9303659184ec0acwrowe by stripping off the directory path (including a trailing slash)
5ac28f3fe2417368757f29cf381338357605fd52wrowe before applying the rules. ) If a substitution is made, a new internal
e1ad80c048e29e968221817698529d73098f07a4wrowe subrequest is issued with the new URL, which restarts processing of the
5123f066275e7d29f5779cb6c9303659184ec0acwrowe request phases. If the substution is a relative path, the <code class="directive"><a href="/mod/mod_rewrite.html#rewritebase">RewriteBase</a></code> directive
5123f066275e7d29f5779cb6c9303659184ec0acwrowe determines the URL-path prefix appended to the substitution.
5123f066275e7d29f5779cb6c9303659184ec0acwrowe In per-directory context, care must be taken to
37ad54b8fd2611b7a4f2b269eec3d27ed784a25dwrowe create rules which will eventually (in some future "round" of per-directory
5123f066275e7d29f5779cb6c9303659184ec0acwrowe rewrite processing) not perform a substitution to avoid looping.
5123f066275e7d29f5779cb6c9303659184ec0acwrowe (See <a href="http://wiki.apache.org/httpd/RewriteLooping">RewriteLooping</a>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe for further discussion of this problem.)</p>
5ac28f3fe2417368757f29cf381338357605fd52wrowe <p>Because of this further manipulation of the URL in per-directory
5ac28f3fe2417368757f29cf381338357605fd52wrowe context, you'll need to take care to craft your rewrite rules
5123f066275e7d29f5779cb6c9303659184ec0acwrowe differently in that context. In particular, remember that the
5123f066275e7d29f5779cb6c9303659184ec0acwrowe leading directory path will be stripped off of the URL that your
5123f066275e7d29f5779cb6c9303659184ec0acwrowe rewrite rules will see. Consider the examples below for further
5123f066275e7d29f5779cb6c9303659184ec0acwrowe clarification.</p>
5123f066275e7d29f5779cb6c9303659184ec0acwrowe <td>RewriteRule ^/images/(.+)\.jpg /images/$1.gif</td>
different contexts, you should consult the <a href="/mod/mod_rewrite.html#logging">log entries</a> made during
through the ruleset rule by rule (<code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> directives) and
<img src="/images/rewrite_rule_flow.png" alt="Flow of RewriteRule and RewriteCond matching" /><br />
<p><span>Available Languages: </span><a href="/en/rewrite/tech.html" title="English"> en </a> |
<a href="/fr/rewrite/tech.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a></p>
<p class="apache">Copyright 2011 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>
<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>