]> granicus.if.org Git - apache/blobdiff - docs/manual/rewrite/advanced.html.en
Re-order listing of pages more logically.
[apache] / docs / manual / rewrite / advanced.html.en
index da18f4e1c69849c27b0c9fd7b01752877ee8215f..2995c59e41b9ac9c009b16a56cb1dce5a2379f48 100644 (file)
@@ -1,30 +1,37 @@
 <?xml version="1.0" encoding="ISO-8859-1"?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
+<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" />
+<!--
         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
               This file is generated from xml source: DO NOT EDIT
         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
       -->
-<title>Advanced Techniques with mod_rewrite - Apache HTTP Server</title>
+<title>Advanced Techniques with mod_rewrite - Apache HTTP Server 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 href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" />
+<script src="../style/scripts/prettify.min.js" type="text/javascript">
+</script>
+
 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
 <body id="manual-page"><div id="page-header">
-<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>
-<p class="apache">Apache HTTP Server Version 2.3</p>
+<p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
+<p class="apache">Apache HTTP Server Version 2.5</p>
 <img alt="" src="../images/feather.gif" /></div>
 <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
 <div id="path">
-<a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.3</a> &gt; <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Advanced Techniques with mod_rewrite</h1>
+<a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.5</a> &gt; <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Advanced Techniques with mod_rewrite</h1>
 <div class="toplang">
-<p><span>Available Languages: </span><a href="../en/rewrite/avoid.html" title="English">&nbsp;en&nbsp;</a></p>
+<p><span>Available Languages: </span><a href="../en/rewrite/advanced.html" title="English">&nbsp;en&nbsp;</a></p>
 </div>
 
 
-<p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> 
-<a href="../mod/mod_rewrite.html">reference documentation</a>. It provides 
-a few advanced techniques and tricks using mod_rewrite.</p>
+<p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
+<a href="../mod/mod_rewrite.html">reference documentation</a>. It provides
+a few advanced techniques using mod_rewrite.</p>
+
+
 
 <div class="warning">Note that many of these examples won't work unchanged in your
 particular server configuration, so it's important that you understand
@@ -32,18 +39,17 @@ them, rather than merely cutting and pasting the examples into your
 configuration.</div>
 
 </div>
-<div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#sharding">URL-based sharding accross multiple backends</a></li>
+<div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#sharding">URL-based sharding across multiple backends</a></li>
 <li><img alt="" src="../images/down.gif" /> <a href="#on-the-fly-content">On-the-fly Content-Regeneration</a></li>
 <li><img alt="" src="../images/down.gif" /> <a href="#load-balancing">Load Balancing</a></li>
-<li><img alt="" src="../images/down.gif" /> <a href="#autorefresh">Document With Autorefresh</a></li>
 <li><img alt="" src="../images/down.gif" /> <a href="#structuredhomedirs">Structured Userdirs</a></li>
 <li><img alt="" src="../images/down.gif" /> <a href="#redirectanchors">Redirecting Anchors</a></li>
 <li><img alt="" src="../images/down.gif" /> <a href="#time-dependent">Time-Dependent Rewriting</a></li>
 <li><img alt="" src="../images/down.gif" /> <a href="#setenvvars">Set Environment Variables Based On URL Parts</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="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="avoid.html">When not to use mod_rewrite</a></li></ul></div>
+</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="avoid.html">When not to use mod_rewrite</a></li></ul><ul class="seealso"><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>
 <div class="section">
-<h2><a name="sharding" id="sharding">URL-based sharding accross multiple backends</a></h2>
+<h2><a name="sharding" id="sharding">URL-based sharding across multiple backends</a></h2>
 
   
 
@@ -51,8 +57,8 @@ configuration.</div>
     <dt>Description:</dt>
 
     <dd>
-      <p>A common technique for distributing the burden of 
-      server load or storage space is called "sharding". 
+      <p>A common technique for distributing the burden of
+      server load or storage space is called "sharding".
       When using this method, a front-end server will use the
       url to consistently "shard" users or objects to separate
       backend servers.</p>
@@ -64,40 +70,40 @@ configuration.</div>
       <p>A mapping is maintained, from users to target servers, in
       external map files. They look like:</p>
 
-<div class="example"><pre>
-user1  physical_host_of_user1
-user2  physical_host_of_user2
+<div class="example"><p><code>
+user1  physical_host_of_user1<br />
+user2  physical_host_of_user2<br />
 :      :
-</pre></div>
+</code></p></div>
 
   <p>We put this into a <code>map.users-to-hosts</code> file. The
     aim is to map;</p>
 
-<div class="example"><pre>
+<div class="example"><p><code>
 /u/user1/anypath
-</pre></div>
+</code></p></div>
 
   <p>to</p>
 
-<div class="example"><pre>
+<div class="example"><p><code>
 http://physical_host_of_user1/u/user/anypath
-</pre></div>
+</code></p></div>
 
       <p>thus every URL path need not be valid on every backend physical
       host. The following ruleset does this for us with the help of the map
       files assuming that server0 is a default server which will be used if
-      a user has no entry in the map):</p>
-
-<div class="example"><pre>
-RewriteEngine on
+      a user has no entry in the map:</p>
 
-RewriteMap      users-to-hosts   txt:/path/to/map.users-to-hosts
+<pre class="prettyprint lang-config">RewriteEngine on
+RewriteMap    users-to-hosts      "txt:/path/to/map.users-to-hosts"
+RewriteRule   "^/u/([^/]+)/?(.*)" "http://${users-to-hosts:$1|server0}/u/$1/$2"</pre>
 
-RewriteRule   ^/u/<strong>([^/]+)</strong>/?(.*)   http://<strong>${users-to-hosts:$1|server0}</strong>/u/$1/$2
-</pre></div>
     </dd>
   </dl>
 
+  <p>See the <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
+  documentation for more discussion of the syntax of this directive.</p>
+
 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
 <div class="section">
 <h2><a name="on-the-fly-content" id="on-the-fly-content">On-the-fly Content-Regeneration</a></h2>
@@ -120,22 +126,23 @@ RewriteRule   ^/u/<strong>([^/]+)</strong>/?(.*)   http://<strong>${users-to-hos
     <dd>
       This is done via the following ruleset:
 
-<div class="example"><pre>
-# This example is valid in per-directory context only
-RewriteCond %{REQUEST_FILENAME}   <strong>!-s</strong>
-RewriteRule ^page\.<strong>html</strong>$          page.<strong>cgi</strong>   [T=application/x-httpd-cgi,L]
-</pre></div>
-
-      <p>Here a request for <code>page.html</code> leads to an
-      internal run of a corresponding <code>page.cgi</code> if
-      <code>page.html</code> is missing or has filesize
-      null. The trick here is that <code>page.cgi</code> is a
-      CGI script which (additionally to its <code>STDOUT</code>)
-      writes its output to the file <code>page.html</code>.
-      Once it has completed, the server sends out
-      <code>page.html</code>. When the webmaster wants to force
-      a refresh of the contents, he just removes
-      <code>page.html</code> (typically from <code>cron</code>).</p>
+<pre class="prettyprint lang-config"># This example is valid in per-directory context only
+RewriteCond "%{REQUEST_URI}"   !-U
+RewriteRule "^(.+)\.html$"     "/regenerate_page.cgi"   [PT,L]</pre>
+
+
+    <p>The <code>-U</code> operator determines whether the test string
+    (in this case, <code>REQUEST_URI</code>) is a valid URL. It does
+    this via a subrequest. In the event that this subrequest fails -
+    that is, the requested resource doesn't exist - this rule invokes
+    the CGI program <code>/regenerate_page.cgi</code>, which generates
+    the requested resource and saves it into the document directory, so
+    that the next time it is requested, a static copy can be served.</p>
+
+    <p>In this way, documents that are infrequently updated can be served in
+    static form. if documents need to be refreshed, they can be deleted
+    from the document directory, and they will then be regenerated the
+    next time they are requested.</p>
     </dd>
   </dl>
 
@@ -159,20 +166,18 @@ RewriteRule ^page\.<strong>html</strong>$          page.<strong>cgi</strong>   [
       <p>We'll use <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code> and a list of servers
       to accomplish this.</p>
 
-<div class="example"><pre>
-RewriteEngine on
-RewriteMap lb rnd:/path/to/serverlist.txt
+<pre class="prettyprint lang-config">RewriteEngine on
+RewriteMap  lb       "rnd:/path/to/serverlist.txt"
+RewriteRule "^/(.*)" "http://${lb:servers}/$1"     [P,L]</pre>
 
-RewriteRule ^/(.*) http://${lb:servers}/$1 [P,L]
-</pre></div>
 
 <p><code>serverlist.txt</code> will contain a list of the servers:</p>
 
-<div class="example"><pre>
-## serverlist.txt
-
-servers one.example.com|two.example.com|three.example.com
-</pre></div>
+<div class="example"><p><code>
+## serverlist.txt<br />
+<br />
+servers one.example.com|two.example.com|three.example.com<br />
+</code></p></div>
 
 <p>If you want one particular server to get more of the load than the
 others, add it more times to the list.</p>
@@ -187,158 +192,6 @@ featureful than anything you can cobble together using mod_rewrite.</p>
    </dd>
   </dl>
 
-</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
-<div class="section">
-<h2><a name="autorefresh" id="autorefresh">Document With Autorefresh</a></h2>
-
-  
-
-  <dl>
-    <dt>Description:</dt>
-
-    <dd>
-      <p>Wouldn't it be nice, while creating a complex web page, if
-      the web browser would automatically refresh the page every
-      time we save a new version from within our editor?
-      Impossible?</p>
-    </dd>
-
-    <dt>Solution:</dt>
-
-    <dd>
-      <p>No! We just combine the MIME multipart feature, the
-      web server NPH feature, and the URL manipulation power of
-      <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>. First, we establish a new
-      URL feature: Adding just <code>:refresh</code> to any
-      URL causes the 'page' to be refreshed every time it is
-      updated on the filesystem.</p>
-
-<div class="example"><pre>
-RewriteRule   ^(/[uge]/[^/]+/?.*):refresh  /internal/cgi/apache/nph-refresh?f=$1
-</pre></div>
-
-      <p>Now when we reference the URL</p>
-
-<div class="example"><pre>
-/u/foo/bar/page.html:refresh
-</pre></div>
-
-      <p>this leads to the internal invocation of the URL</p>
-
-<div class="example"><pre>
-/internal/cgi/apache/nph-refresh?f=/u/foo/bar/page.html
-</pre></div>
-
-      <p>The only missing part is the NPH-CGI script. Although
-      one would usually say "left as an exercise to the reader"
-      ;-) I will provide this, too.</p>
-
-<div class="example"><pre>
-#!/sw/bin/perl
-##
-##  nph-refresh -- NPH/CGI script for auto refreshing pages
-##  Copyright (c) 1997 Ralf S. Engelschall, All Rights Reserved.
-##
-$| = 1;
-
-#   split the QUERY_STRING variable
-@pairs = split(/&amp;/, $ENV{'QUERY_STRING'});
-foreach $pair (@pairs) {
-($name, $value) = split(/=/, $pair);
-$name =~ tr/A-Z/a-z/;
-$name = 'QS_' . $name;
-$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
-eval "\$$name = \"$value\"";
-}
-$QS_s = 1 if ($QS_s eq '');
-$QS_n = 3600 if ($QS_n eq '');
-if ($QS_f eq '') {
-print "HTTP/1.0 200 OK\n";
-print "Content-type: text/html\n\n";
-print "&amp;lt;b&amp;gt;ERROR&amp;lt;/b&amp;gt;: No file given\n";
-exit(0);
-}
-if (! -f $QS_f) {
-print "HTTP/1.0 200 OK\n";
-print "Content-type: text/html\n\n";
-print "&amp;lt;b&amp;gt;ERROR&amp;lt;/b&amp;gt;: File $QS_f not found\n";
-exit(0);
-}
-
-sub print_http_headers_multipart_begin {
-print "HTTP/1.0 200 OK\n";
-$bound = "ThisRandomString12345";
-print "Content-type: multipart/x-mixed-replace;boundary=$bound\n";
-&amp;print_http_headers_multipart_next;
-}
-
-sub print_http_headers_multipart_next {
-print "\n--$bound\n";
-}
-
-sub print_http_headers_multipart_end {
-print "\n--$bound--\n";
-}
-
-sub displayhtml {
-local($buffer) = @_;
-$len = length($buffer);
-print "Content-type: text/html\n";
-print "Content-length: $len\n\n";
-print $buffer;
-}
-
-sub readfile {
-local($file) = @_;
-local(*FP, $size, $buffer, $bytes);
-($x, $x, $x, $x, $x, $x, $x, $size) = stat($file);
-$size = sprintf("%d", $size);
-open(FP, "&amp;lt;$file");
-$bytes = sysread(FP, $buffer, $size);
-close(FP);
-return $buffer;
-}
-
-$buffer = &amp;readfile($QS_f);
-&amp;print_http_headers_multipart_begin;
-&amp;displayhtml($buffer);
-
-sub mystat {
-local($file) = $_[0];
-local($time);
-
-($x, $x, $x, $x, $x, $x, $x, $x, $x, $mtime) = stat($file);
-return $mtime;
-}
-
-$mtimeL = &amp;mystat($QS_f);
-$mtime = $mtime;
-for ($n = 0; $n &amp;lt; $QS_n; $n++) {
-while (1) {
-    $mtime = &amp;mystat($QS_f);
-    if ($mtime ne $mtimeL) {
-        $mtimeL = $mtime;
-        sleep(2);
-        $buffer = &amp;readfile($QS_f);
-        &amp;print_http_headers_multipart_next;
-        &amp;displayhtml($buffer);
-        sleep(5);
-        $mtimeL = &amp;mystat($QS_f);
-        last;
-    }
-    sleep($QS_s);
-}
-}
-
-&amp;print_http_headers_multipart_end;
-
-exit(0);
-
-##EOF##
-</pre></div>
-    </dd>
-  </dl>
-
 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
 <div class="section">
 <h2><a name="structuredhomedirs" id="structuredhomedirs">Structured Userdirs</a></h2>
@@ -364,10 +217,9 @@ exit(0);
       <p>We use the following ruleset to expand the tilde URLs
       into the above layout.</p>
 
-<div class="example"><pre>
-RewriteEngine on
-RewriteRule   ^/~(<strong>([a-z])</strong>[a-z0-9]+)(.*)  /home/<strong>$2</strong>/$1/public_html$3
-</pre></div>
+<pre class="prettyprint lang-config">RewriteEngine on
+RewriteRule   "^/~(<strong>([a-z])</strong>[a-z0-9]+)(.*)"  "/home/<strong>$2</strong>/$1/public_html$3"</pre>
+
     </dd>
   </dl>
 
@@ -396,7 +248,7 @@ RewriteRule   ^/~(<strong>([a-z])</strong>[a-z0-9]+)(.*)  /home/<strong>$2</stro
     </dd>
 
     <dt>Discussion:</dt>
-    <dd>This technique will of course also work with with other 
+    <dd>This technique will of course also work with other
     special characters that mod_rewrite, by default, URL-encodes.</dd>
   </dl>
 
@@ -423,13 +275,12 @@ RewriteRule   ^/~(<strong>([a-z])</strong>[a-z0-9]+)(.*)  /home/<strong>$2</stro
       <code>&gt;STRING</code> and <code>=STRING</code> we can
       do time-dependent redirects:</p>
 
-<div class="example"><pre>
-RewriteEngine on
-RewriteCond   %{TIME_HOUR}%{TIME_MIN} &gt;0700
-RewriteCond   %{TIME_HOUR}%{TIME_MIN} &lt;1900
-RewriteRule   ^foo\.html$             foo.day.html [L]
-RewriteRule   ^foo\.html$             foo.night.html
-</pre></div>
+<pre class="prettyprint lang-config">RewriteEngine on
+RewriteCond   "%{TIME_HOUR}%{TIME_MIN}" &gt;0700
+RewriteCond   "%{TIME_HOUR}%{TIME_MIN}" &lt;1900
+RewriteRule   "^foo\.html$"             "foo.day.html" [L]
+RewriteRule   "^foo\.html$"             "foo.night.html"</pre>
+
 
       <p>This provides the content of <code>foo.day.html</code>
       under the URL <code>foo.html</code> from
@@ -468,25 +319,48 @@ RewriteRule   ^foo\.html$             foo.night.html
     <dd>
       <p>Use the [E] flag to set an environment variable.</p>
 
-<div class="example"><pre>
-RewriteEngine on
-RewriteRule   ^/horse/(.*)   /pony/$1 [E=<strong>rewritten:1</strong>]
-</pre></div>
+<pre class="prettyprint lang-config">RewriteEngine on
+RewriteRule   "^/horse/(.*)"   "/pony/$1" [E=<strong>rewritten:1</strong>]</pre>
+
 
     <p>Later in your ruleset you might check for this environment
     variable using a RewriteCond:</p>
 
-<div class="example"><pre>
-RewriteCond %{ENV:rewritten} =1
-</pre></div>
+<pre class="prettyprint lang-config">RewriteCond "%{ENV:rewritten}"  =1</pre>
+
+
+    <p>Note that environment variables do not survive an external
+    redirect. You might consider using the [CO] flag to set a
+    cookie.</p>
 
     </dd>
   </dl>
 
 </div></div>
 <div class="bottomlang">
-<p><span>Available Languages: </span><a href="../en/rewrite/avoid.html" title="English">&nbsp;en&nbsp;</a></p>
-</div><div id="footer">
-<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>
-<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>
+<p><span>Available Languages: </span><a href="../en/rewrite/advanced.html" title="English">&nbsp;en&nbsp;</a></p>
+</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&amp;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>
+<script type="text/javascript"><!--//--><![CDATA[//><!--
+var comments_shortname = 'httpd';
+var comments_identifier = 'http://httpd.apache.org/docs/trunk/rewrite/advanced.html';
+(function(w, d) {
+    if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
+        d.write('<div id="comments_thread"><\/div>');
+        var s = d.createElement('script');
+        s.type = 'text/javascript';
+        s.async = true;
+        s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
+        (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
+    }
+    else {
+        d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
+    }
+})(window, document);
+//--><!]]></script></div><div id="footer">
+<p class="apache">Copyright 2015 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/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
+if (typeof(prettyPrint) !== 'undefined') {
+    prettyPrint();
+}
+//--><!]]></script>
 </body></html>
\ No newline at end of file