summaryrefslogtreecommitdiff
path: root/docs/manual/rewrite/remapping.html.en
diff options
context:
space:
mode:
Diffstat (limited to 'docs/manual/rewrite/remapping.html.en')
-rw-r--r--docs/manual/rewrite/remapping.html.en312
1 files changed, 168 insertions, 144 deletions
diff --git a/docs/manual/rewrite/remapping.html.en b/docs/manual/rewrite/remapping.html.en
index 98067870..0b97d283 100644
--- a/docs/manual/rewrite/remapping.html.en
+++ b/docs/manual/rewrite/remapping.html.en
@@ -8,10 +8,13 @@
<title>Redirecting and Remapping 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="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
@@ -46,7 +49,7 @@ configuration.</div>
<li><img alt="" src="../images/down.gif" /> <a href="#canonicalurl">Canonical URLs</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#moveddocroot">Moved <code>DocumentRoot</code></a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#fallback-resource">Fallback Resource</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="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>
+</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="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><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="old-to-new" id="old-to-new">From Old to New (internal)</a></h2>
@@ -71,10 +74,11 @@ configuration.</div>
<p>We rewrite the old URL to the new one internally via the
following rule:</p>
-<div class="example"><p><code>
-RewriteEngine on<br />
-RewriteRule ^<strong>/old</strong>\.html$ <strong>/new</strong>.html [PT]
-</code></p></div>
+<pre class="prettyprint lang-config">
+RewriteEngine on
+RewriteRule ^<strong>/foo</strong>\.html$ <strong>/bar</strong>.html [PT]
+</pre>
+
</dd>
</dl>
@@ -102,10 +106,11 @@ RewriteRule ^<strong>/old</strong>\.html$ <strong>/new</strong>.html [PT]
<p>We force a HTTP redirect to the new URL which leads to a
change of the browsers and thus the users view:</p>
-<div class="example"><p><code>
-RewriteEngine on<br />
+<pre class="prettyprint lang-config">
+RewriteEngine on
RewriteRule ^<strong>/foo</strong>\.html$ <strong>bar</strong>.html [<strong>R</strong>]
-</code></p></div>
+</pre>
+
</dd>
<dt>Discussion</dt>
@@ -115,9 +120,8 @@ RewriteRule ^<strong>/foo</strong>\.html$ <strong>bar</strong>.html [<stron
use the Redirect directive. mod_rewrite was used in that earlier
example in order to hide the redirect from the client:</p>
- <div class="example"><p><code>
- Redirect /foo.html /bar.html
- </code></p></div>
+ <pre class="prettyprint lang-config">Redirect /foo.html /bar.html</pre>
+
</dd>
</dl>
@@ -144,18 +148,21 @@ RewriteRule ^<strong>/foo</strong>\.html$ <strong>bar</strong>.html [<stron
to the new server, but you might also consider using the Redirect
or RedirectMatch directive.</p>
-<div class="example"><h3>With mod_rewrite</h3><p><code>
-RewriteEngine on<br />
+<pre class="prettyprint lang-config">#With mod_rewrite
+RewriteEngine on
RewriteRule ^/docs/(.+) http://new.example.com/docs/$1 [R,L]
-</code></p></div>
+</pre>
-<div class="example"><h3>With RedirectMatch</h3><p><code>
+
+<pre class="prettyprint lang-config">#With RedirectMatch
RedirectMatch ^/docs/(.*) http://new.example.com/docs/$1
-</code></p></div>
+</pre>
-<div class="example"><h3>With Redirect</h3><p><code>
+
+<pre class="prettyprint lang-config">#With Redirect
Redirect /docs/ http://new.example.com/docs/
-</code></p></div>
+</pre>
+
</dd>
</dl>
@@ -185,11 +192,12 @@ Redirect /docs/ http://new.example.com/docs/
internally leads to the invocation of
<code>/~quux/foo.cgi</code>.</p>
-<div class="example"><p><code>
-RewriteEngine on<br />
-RewriteBase /~quux/<br />
-RewriteRule ^foo\.<strong>html</strong>$ foo.<strong>cgi</strong> [H=<strong>cgi-script</strong>]
-</code></p></div>
+<pre class="prettyprint lang-config">
+RewriteEngine on
+RewriteBase /~quux/
+RewriteRule ^foo\.html$ foo.cgi &nbsp; [H=<strong>cgi-script</strong>]
+</pre>
+
</dd>
</dl>
@@ -216,21 +224,20 @@ RewriteRule ^foo\.<strong>html</strong>$ foo.<strong>cgi</strong> [H=<stron
existence of the new extension. If it exists, we take
that name, else we rewrite the URL to its original state.</p>
-<div class="example"><p><code>
-# backward compatibility ruleset for<br />
-# rewriting document.html to document.php<br />
-# when and only when document.php exists<br />
-&lt;Directory /var/www/htdocs&gt;<br />
-<span class="indent">
-RewriteEngine on<br />
-RewriteBase /var/www/htdocs<br />
-<br />
-RewriteCond $1.php -f<br />
-RewriteCond $1.html !-f<br />
-RewriteRule ^(.*).html$ $1.php<br />
-</span>
+<pre class="prettyprint lang-config">
+# backward compatibility ruleset for
+# rewriting document.html to document.php
+# when and only when document.php exists
+&lt;Directory /var/www/htdocs&gt;
+ RewriteEngine on
+ RewriteBase /var/www/htdocs
+
+ RewriteCond $1.php -f
+ RewriteCond $1.html !-f
+ RewriteRule ^(.*).html$ $1.php
&lt;/Directory&gt;
-</code></p></div>
+</pre>
+
</dd>
<dt>Discussion</dt>
@@ -277,64 +284,61 @@ but rather uses the <code class="directive"><a href="../mod/mod_alias.html#redir
directive placed in a virtual host for the non-canonical
hostname(s).</p>
-<div class="example"><p><code>
-&lt;VirtualHost *:80&gt;<br />
-<span class="indent">
- ServerName undesired.example.com<br />
- ServerAlias example.com notthis.example.com<br />
-<br />
- Redirect / http://www.example.com/<br />
-</span>
-&lt;/VirtualHost&gt;<br />
-<br />
-&lt;VirtualHost *:80&gt;<br />
-<span class="indent">
- ServerName www.example.com<br />
-</span>
+<pre class="prettyprint lang-config">
+&lt;VirtualHost *:80&gt;
+ ServerName undesired.example.com
+ ServerAlias example.com notthis.example.com
+
+ Redirect / http://www.example.com/
&lt;/VirtualHost&gt;
-</code></p></div>
+
+&lt;VirtualHost *:80&gt;
+ ServerName www.example.com
+&lt;/VirtualHost&gt;
+</pre>
+
<p>You can alternatively accomplish this using the
<code class="directive"><a href="../mod/core.html#if">&lt;If&gt;</a></code>
directive:</p>
-<div class="example"><p><code>
-&lt;If "%{HTTP_HOST} != 'www.example.com'"&gt;<br />
-<span class="indent">
-Redirect / http://www.example.com/
-</span>
+<pre class="prettyprint lang-config">
+&lt;If "%{HTTP_HOST} != 'www.example.com'"&gt;
+ Redirect / http://www.example.com/
&lt;/If&gt;
-</code></p></div>
+</pre>
+
<p>Or, for example, to redirect a portion of your site to HTTPS, you
might do the following:</p>
-<div class="example"><p><code>
-&lt;If "%{SERVER_PROTOCOL} != 'HTTPS'"&gt;<br />
-<span class="indent">
-Redirect /admin/ https://www.example.com/admin/
-</span>
+<pre class="prettyprint lang-config">
+&lt;If "%{SERVER_PROTOCOL} != 'HTTPS'"&gt;
+ Redirect /admin/ https://www.example.com/admin/
&lt;/If&gt;
-</code></p></div>
+</pre>
+
<p>If, for whatever reason, you still want to use <code>mod_rewrite</code>
- if, for example, you need this to work with a larger set of RewriteRules -
you might use one of the recipes below.</p>
<p>For sites running on a port other than 80:</p>
-<div class="example"><p><code>
-RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]<br />
-RewriteCond %{HTTP_HOST} !^$<br />
-RewriteCond %{SERVER_PORT} !^80$<br />
+<pre class="prettyprint lang-config">
+RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
+RewriteCond %{HTTP_HOST} !^$
+RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/?(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]
-</code></p></div>
+</pre>
+
<p>And for a site running on port 80</p>
-<div class="example"><p><code>
-RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]<br />
-RewriteCond %{HTTP_HOST} !^$<br />
+<pre class="prettyprint lang-config">
+RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
+RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
-</code></p></div>
+</pre>
+
<p>
If you wanted to do this generically for all domain names - that
@@ -343,11 +347,12 @@ RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
<strong>example.com</strong>, you could use the following
recipe:</p>
-<div class="example"><p><code>
-RewriteCond %{HTTP_HOST} !^www\. [NC]<br />
-RewriteCond %{HTTP_HOST} !^$<br />
+<pre class="prettyprint lang-config">
+RewriteCond %{HTTP_HOST} !^www\. [NC]
+RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
-</code></p></div>
+</pre>
+
<p>These rulesets will work either in your main server configuration
file, or in a <code>.htaccess</code> file placed in the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code> of the server.</p>
@@ -377,23 +382,24 @@ RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
resource, and, if not finding it in either place, will attempt to
just serve it out of the location requested.</p>
-<div class="example"><p><code>
-RewriteEngine on<br />
-<br />
-# first try to find it in dir1/...<br />
-# ...and if found stop and be happy:<br />
-RewriteCond %{DOCUMENT_ROOT}/<strong>dir1</strong>/%{REQUEST_URI} -f<br />
-RewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir1</strong>/$1 [L]<br />
-<br />
-# second try to find it in dir2/...<br />
-# ...and if found stop and be happy:<br />
-RewriteCond %{DOCUMENT_ROOT}/<strong>dir2</strong>/%{REQUEST_URI} -f<br />
-RewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir2</strong>/$1 [L]<br />
-<br />
-# else go on for other Alias or ScriptAlias directives,<br />
-# etc.<br />
+<pre class="prettyprint lang-config">
+RewriteEngine on
+
+# first try to find it in dir1/...
+# ...and if found stop and be happy:
+RewriteCond %{DOCUMENT_ROOT}/<strong>dir1</strong>/%{REQUEST_URI} -f
+RewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir1</strong>/$1 [L]
+
+# second try to find it in dir2/...
+# ...and if found stop and be happy:
+RewriteCond %{DOCUMENT_ROOT}/<strong>dir2</strong>/%{REQUEST_URI} -f
+RewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir2</strong>/$1 [L]
+
+# else go on for other Alias or ScriptAlias directives,
+# etc.
RewriteRule ^ - [PT]
-</code></p></div>
+</pre>
+
</dd>
</dl>
@@ -421,13 +427,14 @@ RewriteRule ^ - [PT]
<p>We'll use a <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
directive to build a list of servers that we wish to use.</p>
-<div class="example"><p><code>
-HostnameLookups on<br />
-RewriteEngine on<br />
-RewriteMap multiplex txt:/path/to/map.mirrors<br />
-RewriteCond %{REMOTE_HOST} ([a-z]+)$ [NC]<br />
+<pre class="prettyprint lang-config">
+HostnameLookups on
+RewriteEngine on
+RewriteMap multiplex txt:/path/to/map.mirrors
+RewriteCond %{REMOTE_HOST} ([a-z]+)$ [NC]
RewriteRule ^/(.*)$ ${multiplex:<strong>%1</strong>|http://www.example.com/}$1 [R,L]
-</code></p></div>
+</pre>
+
<div class="example"><p><code>
## map.mirrors -- Multiplexing Map<br />
@@ -481,16 +488,17 @@ com http://www.example.com/<br />
All other browsers receive page <code>foo.32.html</code>.
This is done with the following ruleset:</p>
-<div class="example"><p><code>
-RewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/3</strong>.*<br />
-RewriteRule ^foo\.html$ foo.<strong>NS</strong>.html [<strong>L</strong>]<br />
-<br />
-RewriteCond %{HTTP_USER_AGENT} ^<strong>Lynx/</strong> [OR]<br />
-RewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/[12]</strong><br />
-RewriteRule ^foo\.html$ foo.<strong>20</strong>.html [<strong>L</strong>]<br />
-<br />
+<pre class="prettyprint lang-config">
+RewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/3</strong>.*
+RewriteRule ^foo\.html$ foo.<strong>NS</strong>.html [<strong>L</strong>]
+
+RewriteCond %{HTTP_USER_AGENT} ^Lynx/ [OR]
+RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12]
+RewriteRule ^foo\.html$ foo.<strong>20</strong>.html [<strong>L</strong>]
+
RewriteRule ^foo\.html$ foo.<strong>32</strong>.html [<strong>L</strong>]
-</code></p></div>
+</pre>
+
</dd>
</dl>
@@ -521,9 +529,8 @@ RewriteRule ^foo\.html$ foo.<strong>32</strong>.html [<strong>L
we replace <code>/puppies</code> and <code>/canines</code>
by the canonical <code>/dogs</code>.</p>
-<div class="example"><p><code>
-RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R]
-</code></p></div>
+<pre class="prettyprint lang-config">RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R]</pre>
+
</dd>
<dt>Discussion:</dt>
@@ -531,9 +538,8 @@ RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R]
This should really be accomplished with Redirect or RedirectMatch
directives:
- <div class="example"><p><code>
- RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2
- </code></p></div>
+ <pre class="prettyprint lang-config"> RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2 </pre>
+
</dd>
</dl>
@@ -562,16 +568,16 @@ using the following ruleset:</p>
<code>/about/</code>:
</p>
-<div class="example"><p><code>
-RewriteEngine on<br />
-RewriteRule <strong>^/$</strong> /about/ [<strong>R</strong>]
-</code></p></div>
+<pre class="prettyprint lang-config">
+RewriteEngine on
+RewriteRule ^/$ /about/ [<strong>R</strong>]
+</pre>
+
<p>Note that this can also be handled using the <code class="directive"><a href="../mod/mod_alias.html#redirectmatch">RedirectMatch</a></code> directive:</p>
-<div class="example"><p><code>
-RedirectMatch ^/$ http://example.com/about/
-</code></p></div>
+<pre class="prettyprint lang-config">RedirectMatch ^/$ http://example.com/about/</pre>
+
<p>Note also that the example rewrites only the root URL. That is, it
rewrites a request for <code>http://example.com/</code>, but not a
@@ -599,36 +605,33 @@ that should go to an existing resource such as an image, or a css file.</dd>
<dd>
<p>As of version 2.2.16, you should use the <code class="directive"><a href="../mod/mod_dir.html#fallbackresource">FallbackResource</a></code> directive for this:</p>
-<div class="example"><p><code>
-&lt;Directory /var/www/my_blog&gt;<br />
-<span class="indent">
- FallbackResource index.php<br />
-</span>
+<pre class="prettyprint lang-config">
+&lt;Directory /var/www/my_blog&gt;
+ FallbackResource index.php
&lt;/Directory&gt;
-</code></p></div>
+</pre>
+
<p>However, in earlier versions of Apache, or if your needs are more
complicated than this, you can use a variation of the following rewrite
set to accomplish the same thing:</p>
-<div class="example"><p><code>
-&lt;Directory /var/www/my_blog&gt;<br />
-<span class="indent">
- RewriteBase /my_blog<br />
-<br />
- RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-f<br />
- RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-d<br />
- RewriteRule ^ index.php [PT]<br />
-</span>
+<pre class="prettyprint lang-config">
+&lt;Directory /var/www/my_blog&gt;
+ RewriteBase /my_blog
+
+ RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-f
+ RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-d
+ RewriteRule ^ index.php [PT]
&lt;/Directory&gt;
-</code></p></div>
+</pre>
+
<p>If, on the other hand, you wish to pass the requested URI as a query
string argument to index.php, you can replace that RewriteRule with:</p>
-<div class="example"><p><code>
- RewriteRule (.*) index.php?$1 [PT,QSA]
-</code></p></div>
+<pre class="prettyprint lang-config">RewriteRule (.*) index.php?$1 [PT,QSA]</pre>
+
<p>Note that these rulesets can be used in a <code>.htaccess</code>
file, as well as in a &lt;Directory&gt; block.</p>
@@ -640,7 +643,28 @@ file, as well as in a &lt;Directory&gt; block.</p>
</div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="../en/rewrite/remapping.html" title="English">&nbsp;en&nbsp;</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&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/2.4/rewrite/remapping.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