From: Rich Bowen
+
RewriteRule ^/games /usr/local/games/web
-
This maps a request to an arbitrary location on your filesystem, much
like the Alias
directive.
+
RewriteRule ^/foo$ /bar
-
If DocumentRoot
is set
to /usr/local/apache2/htdocs
, then this directive would
map requests for http://example.com/foo
to the
@@ -206,9 +208,10 @@ path /usr/local/apache2/htdocs/bar
.
+
RewriteRule ^/product/view$ http://site2.example.com/seeproduct.html [R]
-
This tells the client to make a new request for the specified URL.
The Substitution can also contain back-references to parts of the incoming URL-path matched by the Pattern. Consider the following:
-
+
RewriteRule ^/product/(.*)/view$ /var/web/productdb/$1
-
The variable $1
will be replaced with whatever text
was matched by the expression inside the parenthesis in
the Pattern. For example, a request
@@ -239,9 +243,10 @@ application of one or more flags to the end of the rule. For example, the
matching behavior of a rule can be made case-insensitive by the
application of the [NC]
flag:
+
RewriteRule ^puppy.html smalldog.html [NC]
-
For more details on the available flags, their meanings, and examples, see the Rewrite Flags document.
@@ -265,10 +270,11 @@ argument is a list of flags that modify how the match is evaluated.For example, to send all requests from a particular IP range to a different server, you could use:
-
-RewriteCond %{REMOTE_ADDR} ^10\.2\.
+
+RewriteCond %{REMOTE_ADDR} ^10\.2\.
RewriteRule (.*) http://intranet.example.com$1
-
When more than
one RewriteCond
is
@@ -277,11 +283,12 @@ the Rewrit
applied. For example, to deny requests that contain the word "hack" in
their query string, unless they also contain a cookie containing
the word "go", you could use:
-RewriteCond %{QUERY_STRING} hack
-RewriteCond %{HTTP_COOKIE} !go
+
+RewriteCond %{QUERY_STRING} hack
+RewriteCond %{HTTP_COOKIE} !go
RewriteRule . - [F]
-
Notice that the exclamation mark specifies a negative match, so the rule is only applied if the cookie does not contain "go".
Matches in the regular expressions contained in
@@ -291,10 +298,11 @@ the Rewrit
variables
%1
, %2
, etc. For example, this
will direct the request to a different directory depending on the
hostname used to access the site:
-RewriteCond %{HTTP_HOST} (.*)
+
+RewriteCond %{HTTP_HOST} (.*)
RewriteRule ^/(.*) /sites/%1/$1
-
If the request was for http://example.com/foo/bar
,
then %1
would contain example.com
and $1
would contain foo/bar
.
This maps a request to an arbitrary location on your filesystem, much
like the
If /usr/local/apache2/htdocs
, then this directive would
map requests for http://example.com/foo
to the
@@ -212,9 +212,9 @@ path /usr/local/apache2/htdocs/bar
.
This tells the client to make a new request for the specified URL.
The Substitution can also contain back-references to parts of the incoming URL-path matched by the Pattern. Consider the following:
-The variable $1
will be replaced with whatever text
was matched by the expression inside the parenthesis in
the Pattern. For example, a request
@@ -246,9 +246,9 @@ application of one or more flags to the end of the rule. For example, the
matching behavior of a rule can be made case-insensitive by the
application of the [NC]
flag:
For more details on the available flags, their meanings, and examples, see the Rewrite Flags document.
@@ -274,10 +274,10 @@ argument is a list of flags that modify how the match is evaluated.For example, to send all requests from a particular IP range to a different server, you could use:
-When more than
one
Notice that the exclamation mark specifies a negative match, so the rule is only applied if the cookie does not contain "go".
Matches in the regular expressions contained in
@@ -300,10 +300,10 @@ the %1
, %2
, etc. For example, this
will direct the request to a different directory depending on the
hostname used to access the site:
If the request was for http://example.com/foo/bar
,
then %1
would contain example.com
and $1
would contain foo/bar
.