<p>We rewrite the old URL to the new one internally via the
following rule:</p>
-<example><pre>
-RewriteEngine on
+<example>
+RewriteEngine on<br />
RewriteRule ^<strong>/old</strong>\.html$ <strong>/new</strong>.html [PT]
-</pre></example>
+</example>
</dd>
</dl>
<p>We force a HTTP redirect to the new URL which leads to a
change of the browsers and thus the users view:</p>
-<example><pre>
-RewriteEngine on
+<example>
+RewriteEngine on<br />
RewriteRule ^<strong>/foo</strong>\.html$ <strong>bar</strong>.html [<strong>R</strong>]
-</pre></example>
+</example>
</dd>
<dt>Discussion</dt>
to the new server, but you might also consider using the Redirect
or RedirectMatch directive.</p>
-<example><title>With mod_rewrite</title><pre>
-RewriteEngine on
+<example><title>With mod_rewrite</title>
+RewriteEngine on<br />
RewriteRule ^/docs/(.+) http://new.example.com/docs/$1 [R,L]
-</pre></example>
+</example>
-<example><title>With RedirectMatch</title><pre>
+<example><title>With RedirectMatch</title>
RedirectMatch ^/docs/(.*) http://new.example.com/docs/$1
-</pre></example>
+</example>
<example><title>With Redirect</title><pre>
Redirect /docs/ http://new.example.com/docs/
-</pre></example>
+</example>
</dd>
</dl>
internally leads to the invocation of
<code>/~quux/foo.cgi</code>.</p>
-<example><pre>
-RewriteEngine on
-RewriteBase /~quux/
+<example>
+RewriteEngine on<br />
+RewriteBase /~quux/<br />
RewriteRule ^foo\.<strong>html</strong>$ foo.<strong>cgi</strong> [H=<strong>cgi-script</strong>]
-</pre></example>
+</example>
</dd>
</dl>
existence of the new extension. If it exists, we take
that name, else we rewrite the URL to its original state.</p>
-<example><pre>
-# backward compatibility ruleset for
-# rewriting document.html to document.php
-# when and only when document.php exists
-<Directory /var/www/htdocs>
-RewriteEngine on
-RewriteBase /var/www/htdocs
-
-RewriteCond $1.php -f
-RewriteCond $1.html !-f
-RewriteRule ^(.*).html$ $1.php
+<example>
+# backward compatibility ruleset for<br />
+# rewriting document.html to document.php<br />
+# when and only when document.php exists<br />
+<Directory /var/www/htdocs><br />
+<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 />
+</indent>
</Directory>
-</pre></example>
+</example>
</dd>
<dt>Discussion</dt>
you might use one of the recipes below.</p>
<p>For sites running on a port other than 80:</p>
-<example><pre>
-RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
-RewriteCond %{HTTP_HOST} !^$
-RewriteCond %{SERVER_PORT} !^80$
+<example>
+RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]<br />
+RewriteCond %{HTTP_HOST} !^$<br />
+RewriteCond %{SERVER_PORT} !^80$<br />
RewriteRule ^/?(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]
-</pre></example>
+</example>
<p>And for a site running on port 80</p>
-<example><pre>
-RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
-RewriteCond %{HTTP_HOST} !^$
+<example>
+RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]<br />
+RewriteCond %{HTTP_HOST} !^$<br />
RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
-</pre></example>
+</example>
<p>
If you wanted to do this generically for all domain names - that
<strong>example.com</strong>, you could use the following
recipe:</p>
-<example><pre>
-RewriteCond %{HTTP_HOST} !^www\. [NC]
-RewriteCond %{HTTP_HOST} !^$
+<example>
+RewriteCond %{HTTP_HOST} !^www\. [NC]<br />
+RewriteCond %{HTTP_HOST} !^$<br />
RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
-</pre></example>
+</example>
<p>These rulesets will work either in your main server configuration
file, or in a <code>.htaccess</code> file placed in the <directive
resource, and, if not finding it in either place, will attempt to
just serve it out of the location requested.</p>
-<example><pre>
-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.
+<example>
+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 />
RewriteRule ^ - [PT]
-</pre></example>
+</example>
</dd>
</dl>
<p>We'll use a <directive module="mod_rewrite">RewriteMap</directive>
directive to build a list of servers that we wish to use.</p>
-<example><pre>
-HostnameLookups on
-RewriteEngine on
-RewriteMap multiplex txt:/path/to/map.mirrors
-RewriteCond %{REMOTE_HOST} ([a-z]+)$ [NC]
+<example>
+HostnameLookups on<br />
+RewriteEngine on<br />
+RewriteMap multiplex txt:/path/to/map.mirrors<br />
+RewriteCond %{REMOTE_HOST} ([a-z]+)$ [NC]<br />
RewriteRule ^/(.*)$ ${multiplex:<strong>%1</strong>|http://www.example.com/}$1 [R,L]
-</pre></example>
-
-<example><pre>
-## map.mirrors -- Multiplexing Map
+</example>
-de http://www.example.de/
-uk http://www.example.uk/
-com http://www.example.com/
+<example>
+## map.mirrors -- Multiplexing Map<br />
+<br />
+de http://www.example.de/<br />
+uk http://www.example.uk/<br />
+com http://www.example.com/<br />
##EOF##
-</pre></example>
+</example>
</dd>
<dt>Discussion</dt>
All other browsers receive page <code>foo.32.html</code>.
This is done with the following ruleset:</p>
-<example><pre>
-RewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/3</strong>.*
-RewriteRule ^foo\.html$ foo.<strong>NS</strong>.html [<strong>L</strong>]
-
-RewriteCond %{HTTP_USER_AGENT} ^<strong>Lynx/</strong>.* [OR]
-RewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/[12]</strong>.*
-RewriteRule ^foo\.html$ foo.<strong>20</strong>.html [<strong>L</strong>]
-
+<example>
+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 />
RewriteRule ^foo\.html$ foo.<strong>32</strong>.html [<strong>L</strong>]
-</pre></example>
+</example>
</dd>
</dl>
we replace <code>/puppies</code> and <code>/canines</code>
by the canonical <code>/dogs</code>.</p>
-<example><pre>
+<example>
RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R]
-</pre></example>
+</example>
</dd>
<dt>Discussion:</dt>
This should really be accomplished with Redirect or RedirectMatch
directives:
- <example><pre>
+ <example>
RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2
- </pre></example>
+ </example>
</dd>
</dl>
<code>/about/</code>:
</p>
-<example><pre>
-RewriteEngine on
+<example>
+RewriteEngine on<br />
RewriteRule <strong>^/$</strong> /about/ [<strong>R</strong>]
-</pre></example>
+</example>
<p>Note that this can also be handled using the <directive
module="mod_alias">RedirectMatch</directive> directive:</p>
module="mod_dir">FallbackResource</directive> directive for this:</p>
<example>
-<pre>
-<Directory /var/www/my_blog>
- FallbackResource index.php
+<Directory /var/www/my_blog><br />
+<indent>
+ FallbackResource index.php<br />
+</indent>
</Directory>
-</pre>
</example>
<p>However, in earlier versions of Apache, or if your needs are more
set to accomplish the same thing:</p>
<example>
-<pre>
-<Directory /var/www/my_blog>
- RewriteBase /my_blog
-
- RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-f
- RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-d
- RewriteRule ^ index.php [PT]
+<Directory /var/www/my_blog><br />
+<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 />
+<indent>
</Directory>
-</pre>
</example>
<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>
<example>
-<pre>
RewriteRule (.*) index.php?$1 [PT,QSA]
-</pre>
</example>
<p>Note that these rulesets can be uses in a <code>.htaccess</code>