<highlight language="config">
RewriteEngine on
-RewriteRule ^<strong>/foo</strong>\.html$ <strong>/bar</strong>.html [PT]
+RewriteRule "^<strong>/foo</strong>\.html$" "<strong>/bar</strong>.html" [PT]
</highlight>
</dd>
</dl>
<highlight language="config">
RewriteEngine on
-RewriteRule ^<strong>/foo</strong>\.html$ <strong>bar</strong>.html [<strong>R</strong>]
+RewriteRule "^<strong>/foo</strong>\.html$" "<strong>bar</strong>.html" [<strong>R</strong>]
</highlight>
</dd>
use the Redirect directive. mod_rewrite was used in that earlier
example in order to hide the redirect from the client:</p>
- <highlight language="config">Redirect /foo.html /bar.html</highlight>
+ <highlight language="config">
+Redirect "/foo.html" "/bar.html"
+ </highlight>
</dd>
</dl>
to the new server, but you might also consider using the Redirect
or RedirectMatch directive.</p>
-<highlight language="config">#With mod_rewrite
+<highlight language="config">
+#With mod_rewrite
RewriteEngine on
-RewriteRule ^/docs/(.+) http://new.example.com/docs/$1 [R,L]
+RewriteRule "^/docs/(.+)" "http://new.example.com/docs/$1" [R,L]
</highlight>
-<highlight language="config">#With RedirectMatch
-RedirectMatch ^/docs/(.*) http://new.example.com/docs/$1
+<highlight language="config">
+#With RedirectMatch
+RedirectMatch "^/docs/(.*)" "http://new.example.com/docs/$1"
</highlight>
-<highlight language="config">#With Redirect
-Redirect /docs/ http://new.example.com/docs/
+<highlight language="config">
+#With Redirect
+Redirect "/docs/" "http://new.example.com/docs/"
</highlight>
</dd>
</dl>
<highlight language="config">
RewriteEngine on
-RewriteBase /~quux/
-RewriteRule ^foo\.html$ foo.cgi [H=<strong>cgi-script</strong>]
+RewriteBase "/~quux/"
+RewriteRule "^foo\.html$" "foo.cgi" [H=<strong>cgi-script</strong>]
</highlight>
</dd>
</dl>
# backward compatibility ruleset for
# rewriting document.html to document.php
# when and only when document.php exists
-<Directory /var/www/htdocs>
+<Directory "/var/www/htdocs">
RewriteEngine on
- RewriteBase /var/www/htdocs
+ RewriteBase "/var/www/htdocs"
- RewriteCond $1.php -f
- RewriteCond $1.html !-f
- RewriteRule ^(.*).html$ $1.php
+ RewriteCond "$1.php" -f
+ RewriteCond "$1.html" !-f
+ RewriteRule "^(.*).html$" "$1.php"
</Directory>
</highlight>
</dd>
ServerName undesired.example.com
ServerAlias example.com notthis.example.com
- Redirect / http://www.example.com/
+ Redirect "/" "http://www.example.com/"
</VirtualHost>
<VirtualHost *:80>
<p>You can alternatively accomplish this using the
<directive module="core" type="section">If</directive>
-directive:</p>
+directive: (<strong>2.4 and later</strong>)</p>
<highlight language="config">
<If "%{HTTP_HOST} != 'www.example.com'">
- Redirect / http://www.example.com/
+ Redirect "/" "http://www.example.com/"
</If>
</highlight>
<highlight language="config">
<If "%{SERVER_PROTOCOL} != 'HTTPS'">
- Redirect /admin/ https://www.example.com/admin/
+ Redirect "/admin/" "https://www.example.com/admin/"
</If>
</highlight>
<p>For sites running on a port other than 80:</p>
<highlight language="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]
+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]
</highlight>
<p>And for a site running on port 80</p>
<highlight language="config">
-RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
-RewriteCond %{HTTP_HOST} !^$
-RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
+RewriteCond "%{HTTP_HOST}" "!^www\.example\.com" [NC]
+RewriteCond "%{HTTP_HOST}" "!^$"
+RewriteRule "^/?(.*)" "http://www.example.com/$1" [L,R,NE]
</highlight>
<p>
recipe:</p>
<highlight language="config">
-RewriteCond %{HTTP_HOST} !^www\. [NC]
-RewriteCond %{HTTP_HOST} !^$
-RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
+RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
+RewriteCond "%{HTTP_HOST}" "!^$"
+RewriteRule "^/?(.*)" "http://www.%{HTTP_HOST}/$1" [L,R,NE]
</highlight>
<p>These rulesets will work either in your main server configuration
# 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]
+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]
+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]
+RewriteRule "^" "-" [PT]
</highlight>
</dd>
</dl>
<highlight language="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]
+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]
</highlight>
<example>
we replace <code>/puppies</code> and <code>/canines</code>
by the canonical <code>/dogs</code>.</p>
-<highlight language="config">RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R]</highlight>
+<highlight language="config">
+RewriteRule "^/(puppies|canines)/(.*)" "/dogs/$2" [R]
+</highlight>
</dd>
<dt>Discussion:</dt>
This should really be accomplished with Redirect or RedirectMatch
directives:
- <highlight language="config"> RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2 </highlight>
+ <highlight language="config">
+RedirectMatch "^/(puppies|canines)/(.*)" "/dogs/$2"
+ </highlight>
</dd>
</dl>
<highlight language="config">
RewriteEngine on
-RewriteRule ^/$ /about/ [<strong>R</strong>]
+RewriteRule "^/$" "/about/" [<strong>R</strong>]
</highlight>
<p>Note that this can also be handled using the <directive
module="mod_alias">RedirectMatch</directive> directive:</p>
-<highlight language="config">RedirectMatch ^/$ http://example.com/about/</highlight>
+<highlight language="config">
+RedirectMatch "^/$" "http://example.com/about/"
+</highlight>
<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
module="mod_dir">FallbackResource</directive> directive for this:</p>
<highlight language="config">
-<Directory /var/www/my_blog>
+<Directory "/var/www/my_blog">
FallbackResource index.php
</Directory>
</highlight>
set to accomplish the same thing:</p>
<highlight language="config">
-<Directory /var/www/my_blog>
- RewriteBase /my_blog
+<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]
+ RewriteCond "/var/www/my_blog/%{REQUEST_FILENAME}" !-f
+ RewriteCond "/var/www/my_blog/%{REQUEST_FILENAME}" !-d
+ RewriteRule "^" "index.php" [PT]
</Directory>
</highlight>
<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>
-<highlight language="config">RewriteRule (.*) index.php?$1 [PT,QSA]</highlight>
+<highlight language="config">
+RewriteRule "(.*)" "index.php?$1" [PT,QSA]
+</highlight>
<p>Note that these rulesets can be used in a <code>.htaccess</code>
file, as well as in a <Directory> block.</p>
<p> Many of the solutions in this section will all use the same condition,
which leaves the matched value in the %2 backreference. %1 is the beginining
of the query string (up to the key of intererest), and %3 is the remainder. This
-condition is a bit complex for flexibility and to avoid double '&&' in the
+condition is a bit complex for flexibility and to avoid double '&&' in the
substitutions.</p>
<ul>
<li>This solution removes the matching key and value:
<highlight language="config">
# Remove mykey=???
-RewriteCond %{QUERY_STRING} (.*(?:^|&))mykey=([^&]*)&?(.*)&?$
-RewriteRule (.*) $1?%1%3
+RewriteCond "%{QUERY_STRING}" "(.*(?:^|&))mykey=([^&]*)&?(.*)&?$"
+RewriteRule "(.*)" "$1?%1%3"
</highlight>
</li>
<highlight language="config">
# Copy from query string to PATH_INFO
-RewriteCond %{QUERY_STRING} (.*(?:^|&))mykey=([^&]*)&?(.*)&?$
-RewriteRule (.*) $1/products/%2/? [PT]
+RewriteCond "%{QUERY_STRING}" "(.*(?:^|&))mykey=([^&]*)&?(.*)&?$"
+RewriteRule "(.*)" "$1/products/%2/?" [PT]
</highlight>
</li>
<highlight language="config">
# Capture the value of mykey in the query string
-RewriteCond %{QUERY_STRING} (.*(?:^|&))mykey=([^&]*)&?(.*)&?$
-RewriteCond %2 !=not-so-secret-value
-RewriteRule (.*) - [F]
+RewriteCond "%{QUERY_STRING}" "(.*(?:^|&))mykey=([^&]*)&?(.*)&?$"
+RewriteCond "%2" !=not-so-secret-value
+RewriteRule "(.*)" "-" [F]
</highlight>
</li>
<li>This solution shows the reverse of the previous ones, copying
path components (perhaps PATH_INFO) from the URL into the query string.
<highlight language="config">
-# The desired URL might be /products/kitchen-sink, and the script expects
+# The desired URL might be /products/kitchen-sink, and the script expects
# /path?products=kitchen-sink.
-RewriteRule ^/?path/([^/]+)/([^/]+) /path?$1=$2 [PT]
+RewriteRule "^/?path/([^/]+)/([^/]+)" "/path?$1=$2" [PT]
</highlight>
</li>
</ul>