diff options
Diffstat (limited to 'docs/manual/rewrite/advanced.html.en')
-rw-r--r-- | docs/manual/rewrite/advanced.html.en | 220 |
1 files changed, 125 insertions, 95 deletions
diff --git a/docs/manual/rewrite/advanced.html.en b/docs/manual/rewrite/advanced.html.en index 497e3bb5..cc4d20ed 100644 --- a/docs/manual/rewrite/advanced.html.en +++ b/docs/manual/rewrite/advanced.html.en @@ -8,10 +8,13 @@ <title>Advanced Techniques with mod_rewrite - Apache HTTP Server</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.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="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> <p class="apache">Apache HTTP Server Version 2.4</p> <img alt="" src="../images/feather.gif" /></div> <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div> @@ -41,7 +44,7 @@ configuration.</div> <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="rewritemap.html">Using RewriteMap</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> @@ -89,13 +92,12 @@ http://physical_host_of_user1/u/user/anypath 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"><p><code> -RewriteEngine on<br /> -<br /> -RewriteMap users-to-hosts txt:/path/to/map.users-to-hosts<br /> -<br /> -RewriteRule ^/u/<strong>([^/]+)</strong>/?(.*) http://<strong>${users-to-hosts:$1|server0}</strong>/u/$1/$2 -</code></p></div> +<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> + </dd> </dl> @@ -124,11 +126,12 @@ RewriteRule ^/u/<strong>([^/]+)</strong>/?(.*) http://<strong>${users-to-hos <dd> This is done via the following ruleset: -<div class="example"><p><code> -# This example is valid in per-directory context only<br /> -RewriteCond %{REQUEST_URI} <strong>!-U</strong><br /> +<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] -</code></p></div> +</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 @@ -165,12 +168,12 @@ RewriteRule ^(.+)\.html$ /regenerate_page.cgi [PT,L] <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"><p><code> -RewriteEngine on<br /> -RewriteMap lb rnd:/path/to/serverlist.txt<br /> -<br /> +<pre class="prettyprint lang-config"> +RewriteEngine on +RewriteMap lb rnd:/path/to/serverlist.txt RewriteRule ^/(.*) http://${lb:servers}/$1 [P,L] -</code></p></div> +</pre> + <p><code>serverlist.txt</code> will contain a list of the servers:</p> @@ -221,9 +224,10 @@ featureful than anything you can cobble together using mod_rewrite.</p> URL causes the 'page' to be refreshed every time it is updated on the filesystem.</p> -<div class="example"><p><code> +<pre class="prettyprint lang-config"> RewriteRule ^(/[uge]/[^/]+/?.*):refresh /internal/cgi/apache/nph-refresh?f=$1 -</code></p></div> +</pre> + <p>Now when we reference the URL</p> @@ -241,7 +245,7 @@ RewriteRule ^(/[uge]/[^/]+/?.*):refresh /internal/cgi/apache/nph-refresh?f=$1 one would usually say "left as an exercise to the reader" ;-) I will provide this, too.</p> -<div class="example"><pre> +<pre class="prettyprint lang-perl"> #!/sw/bin/perl ## ## nph-refresh -- NPH/CGI script for auto refreshing pages @@ -250,61 +254,61 @@ RewriteRule ^(/[uge]/[^/]+/?.*):refresh /internal/cgi/apache/nph-refresh?f=$1 $| = 1; # split the QUERY_STRING variable -@pairs = split(/&/, $ENV{'QUERY_STRING'}); +@pairs = split( /&/, $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\""; + ( $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 "&lt;b&gt;ERROR&lt;/b&gt;: No file given\n"; -exit(0); +$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 "<b>ERROR</b>: 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 "&lt;b&gt;ERROR&lt;/b&gt;: File $QS_f not found\n"; -exit(0); +if ( !-f $QS_f ) { + print "HTTP/1.0 200 OK\n"; + print "Content-type: text/html\n\n"; + print "<b>ERROR</b>: 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"; -&print_http_headers_multipart_next; + print "HTTP/1.0 200 OK\n"; + $bound = "ThisRandomString12345"; + print "Content-type: multipart/x-mixed-replace;boundary=$bound\n"; + &print_http_headers_multipart_next; } sub print_http_headers_multipart_next { -print "\n--$bound\n"; + print "\n--$bound\n"; } sub print_http_headers_multipart_end { -print "\n--$bound--\n"; + 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; + 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, "&lt;$file"); -$bytes = sysread(FP, $buffer, $size); -close(FP); -return $buffer; + local ($file) = @_; + local ( *FP, $size, $buffer, $bytes ); + ( $x, $x, $x, $x, $x, $x, $x, $size ) = stat($file); + $size = sprintf( "%d", $size ); + open( FP, "<$file" ); + $bytes = sysread( FP, $buffer, $size ); + close(FP); + return $buffer; } $buffer = &readfile($QS_f); @@ -312,30 +316,30 @@ $buffer = &readfile($QS_f); &displayhtml($buffer); sub mystat { -local($file) = $_[0]; -local($time); + local ($file) = $_[0]; + local ($time); -($x, $x, $x, $x, $x, $x, $x, $x, $x, $mtime) = stat($file); -return $mtime; + ( $x, $x, $x, $x, $x, $x, $x, $x, $x, $mtime ) = stat($file); + return $mtime; } $mtimeL = &mystat($QS_f); -$mtime = $mtime; -for ($n = 0; $n &lt; $QS_n; $n++) { -while (1) { - $mtime = &mystat($QS_f); - if ($mtime ne $mtimeL) { - $mtimeL = $mtime; - sleep(2); - $buffer = &readfile($QS_f); - &print_http_headers_multipart_next; - &displayhtml($buffer); - sleep(5); - $mtimeL = &mystat($QS_f); - last; +$mtime = $mtime; +for ( $n = 0 ; $n & lt ; $QS_n ; $n++ ) { + while (1) { + $mtime = &mystat($QS_f); + if ( $mtime ne $mtimeL ) { + $mtimeL = $mtime; + sleep(2); + $buffer = &readfile($QS_f); + &print_http_headers_multipart_next; + &displayhtml($buffer); + sleep(5); + $mtimeL = &mystat($QS_f); + last; + } + sleep($QS_s); } - sleep($QS_s); -} } &print_http_headers_multipart_end; @@ -343,7 +347,8 @@ while (1) { exit(0); ##EOF## -</pre></div> +</pre> + </dd> </dl> @@ -372,10 +377,11 @@ exit(0); <p>We use the following ruleset to expand the tilde URLs into the above layout.</p> -<div class="example"><p><code> -RewriteEngine on<br /> +<pre class="prettyprint lang-config"> +RewriteEngine on RewriteRule ^/~(<strong>([a-z])</strong>[a-z0-9]+)(.*) /home/<strong>$2</strong>/$1/public_html$3 -</code></p></div> +</pre> + </dd> </dl> @@ -431,13 +437,14 @@ RewriteRule ^/~(<strong>([a-z])</strong>[a-z0-9]+)(.*) /home/<strong>$2</stro <code>>STRING</code> and <code>=STRING</code> we can do time-dependent redirects:</p> -<div class="example"><p><code> -RewriteEngine on<br /> -RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700<br /> -RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900<br /> -RewriteRule ^foo\.html$ foo.day.html [L]<br /> +<pre class="prettyprint lang-config"> +RewriteEngine on +RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700 +RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900 +RewriteRule ^foo\.html$ foo.day.html [L] RewriteRule ^foo\.html$ foo.night.html -</code></p></div> +</pre> + <p>This provides the content of <code>foo.day.html</code> under the URL <code>foo.html</code> from @@ -476,17 +483,19 @@ RewriteRule ^foo\.html$ foo.night.html <dd> <p>Use the [E] flag to set an environment variable.</p> -<div class="example"><p><code> -RewriteEngine on<br /> +<pre class="prettyprint lang-config"> +RewriteEngine on RewriteRule ^/horse/(.*) /pony/$1 [E=<strong>rewritten:1</strong>] -</code></p></div> +</pre> + <p>Later in your ruleset you might check for this environment variable using a RewriteCond:</p> -<div class="example"><p><code> +<pre class="prettyprint lang-config"> RewriteCond %{ENV:rewritten} =1 -</code></p></div> +</pre> + <p>Note that environment variables do not survive an external redirect. You might consider using the [CO] flag to set a @@ -499,7 +508,28 @@ RewriteCond %{ENV:rewritten} =1 <div class="bottomlang"> <p><span>Available Languages: </span><a href="../en/rewrite/avoid.html" title="English"> en </a> | <a href="../fr/rewrite/avoid.html" hreflang="fr" rel="alternate" title="Français"> fr </a></p> -</div><div id="footer"> +</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> +<script type="text/javascript"><!--//--><![CDATA[//><!-- +var comments_shortname = 'httpd'; +var comments_identifier = 'http://httpd.apache.org/docs/2.4/rewrite/avoid.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 2012 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 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[//><!-- +if (typeof(prettyPrint) !== 'undefined') { + prettyPrint(); +} +//--><!]]></script> </body></html>
\ No newline at end of file |