From: Joshua Slive `` Despite the tons of examples and docs,
+ ``Despite the tons of examples and docs,
mod_rewrite is voodoo. Damned cool voodoo, but still
voodoo.'' This module operates on the full URLs (including the
path-info part) both in per-server context
(
-
httpd.conf
) and per-directory context
- (.htaccess
) and can even generate query-string
+ (.htaccess
files and <Directory>
+ blocks) and can even generate query-string
parts on result. The rewritten result can lead to internal
sub-processing, external request redirection or even to an
internal proxy throughput.
-`` Despite the tons of examples and docs, +
``Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo.''
@@ -64,7 +64,8 @@This module operates on the full URLs (including the path-info part) both in per-server context (
@@ -75,26 +76,21 @@ -httpd.conf
) and per-directory context - (.htaccess
) and can even generate query-string + (.htaccess
files and<Directory>
+ blocks) and can even generate query-string parts on result. The rewritten result can lead to internal sub-processing, external request redirection or even to an internal proxy throughput.mod_rewrite reference -documentation -Introduction -Technical details -Practical solutions to common -problems -Practical solutions to -advanced problems +Mapping URLs to the Filesystem +mod_rewrite +wiki +Glossary diff --git a/docs/manual/rewrite/rewrite_flags.html b/docs/manual/rewrite/rewrite_flags.html new file mode 100644 index 00000000000..605b284a467 --- /dev/null +++ b/docs/manual/rewrite/rewrite_flags.html @@ -0,0 +1,3 @@ +URI: rewrite_flags.html.en +Content-Language: en +Content-type: text/html; charset=ISO-8859-1 diff --git a/docs/manual/rewrite/rewrite_flags.html.en b/docs/manual/rewrite/rewrite_flags.html.en new file mode 100644 index 00000000000..f02a956b2a0 --- /dev/null +++ b/docs/manual/rewrite/rewrite_flags.html.en @@ -0,0 +1,420 @@ + + + + Documentation Apache mod_rewrite Flags - Apache HTTP Server + + + + +++ + +Apache HTTP Server Version 2.2
+++Apache mod_rewrite Flags
+++ +Available Languages:
+This document discusses the flags which are available to the +
+RewriteRule
directive, +providing detailed explanations and examples. This is not necessarily +a comprehensive list of all flags available, so be sure to also +consult the reference documentation.+ +See also
++Introduction
++
RewriteRule
s can have +their behavior modified by one or more flags. Flags are included in +square brackets at the end of the rule, and multiple flags are separated +by commas.+ +
+RewriteRule pattern target [Flag1,Flag2,Flag3] +
The flags all have a short form, such as
+ +CO
, as well as +a longer form, such ascookie
. Some flags take one or more +arguments. Flags are not case sensitive.+The flags
+ +Each flag has a long and short form. While it is most common to use +the short form, it is recommended that you familiarize yourself with the +long form, so that you remember what each flag is supposed to do.
+ +Presented here are each of the available flags, along with an example +of how you might use them.
+ +C|chain
+The [C] or [chain] flag indicates that the
+ + + +RewriteRule
is chained to the next +rule. That is, if the rule matches, then it is processed as usual and +control moves on to the next rule. However, if it does not match, then +the next rule, and any other rules that are chained together, will be +skipped.CO|cookie
+The [CO], or [cookie] flag, allows you to set a cookie when a +particular
+RewriteRule
+matches. The argument consists of three required fields and two optional +fields.You must declare a name and value for the cookie to be set, and the +domain for which you wish the cookie to be valid. You may optionally set +the lifetime of the cookie, and the path for which it should be +returned.
+By default, the lifetime of the cookie is the current browser +session.
+By default, the path for which the cookie will be valid is "/" - that +is, the entire website.
+Several examples are offered here:
+ ++ +
+RewriteEngine On
+RewriteRule ^/index.html - [CO=frontdoor:yes:.apache.org:1440:/] +This rule doesn't rewrite the request (the "-" rewrite target tells +mod_rewrite to pass the request through unchanged) but sets a cookie +called 'frontdoor' to a value of 'yes'. The cookie is valid for any host +in the
+ + + +.apache.org
domain. It will be set to expire in 1440 +minutes (24 hours) and will be returned for all URIs.E|env
+With the [E], or [env] flag, you can set the value of an environment +variable. Note that some environment variables may be set after the rule +is run, thus unsetting what you have set. See the +Environment Variables document for more details on how Environment +variables work.
+ +The following example sets an evironment variable called 'image' to a +value of '1' if the requested URI is an image file. Then, that +environment variable is used to exclude those requests from the access +log.
+ ++ +
+RewriteRule \.(png|gif|jpg) - [E=image:1]
+CustomLog logs/access_log combined env=!image +Note that this same effect can be obtained using
+ + +SetEnvIf
. This technique is offered as +an example, not as a recommendation.F|forbidden
+Using the [F] flag causes Apache to return a 403 Forbidden status +code to the client. While the same behavior can be accomplished using +the
+ +Deny
directive, this +allows more flexibility in assigning a Forbidden status.The following rule will forbid
+ +.exe
files from being +downloaded from your server.+ +
+RewriteRule \.exe - [F] +
This example uses the "-" syntax for the rewrite target, which means +that the requested URI is not modified. There's no reason to rewrite to +another URI, if you're going to forbid the request.
+ + + +G|gone
+The [G] flag forces Apache to return a 410 Gone status with the +response. This indicates that a resource used to be available, but is no +longer available.
+ +As with the [F] flag, you will typically use the "-" syntax for the +rewrite target when using the [G] flag:
+ ++ + +
+RewriteRule oldproduct - [G,NC] +
H|handler
+Forces the resulting request to be handled with the specified +handler. For example, one might use this to force all files without a +file extension to be parsed by the php handler:
+ ++ +
+RewriteRule !\. - [H=application/x-httpd-php] +
+The regular expression above -
+ + +!\.
- will match any request +that does not contain the literal.
character. +L|last
+The [L] flag causes
+ +mod_rewrite
to stop processing +the rule set. In most contexts, this means that if the rule matches, no +further rules will be processed.If you are using
+ +RewriteRule
in either +.htaccess
files or in +<Directory>
sections, +it is important to have some understanding of how the rules are +processed. The simplified form of this is that once the rules have been +processed, the rewritten request is handed back to the URL parsing +engine to do what it may with it. It is possible that as the rewritten +request is handled, the.htaccess
file or +<Directory>
section +may be encountered again, and thus the ruleset may be run again from the +start. Most commonly this will happen if one of the rules causes a +redirect - either internal or external - causing the request process to +start over.It is therefore important, if you are using
+ +RewriteRule
directives in one of these +context that you take explicit steps to avoid rules looping, and not +count solely on the [L] flag to terminate execution of a series of +rules, as shown below.The example given here will rewrite any request to +
+ +index.php
, giving the original request as a query string +argument toindex.php
, however, if the request is already +forindex.php
, this rull will be skipped.+ + +
+RewriteCond %{REQUEST_URI} !index\.php
+RewriteRule ^(.*) index.php?req=$1 [L] +N|next
++The [N] flag causes the ruleset to start over again from the top. Use +with extreme caution, as it may result in loop. +
++The [Next] flag could be used, for example, if you wished to replace a +certain string or letter repeatedly in a request. The example shown here +will replace A with B everywhere in a request, and will continue doing +so until there are no more As to be replaced. +
+ ++ +
+RewriteRule (.*)A(.*) $1B$2 [N] +
You can think of this as a
+ + + +while
loop: While this +pattern still matches, perform this substitution.NC|nocase
+Use of the [NC] flag causes the
+ +RewriteRule
to be matched in a +case-insensitive manner. That is, it doesn't care whether letters appear +as upper-case or lower-case in the matched URI.In the example below, any request for an image file will be proxied +to your dedicated image server. The match is case-insensitive, so that +
+ +.jpg
and.JPG
files are both acceptable, for +example.+ + +
+RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC] +
NE|noescape
+By default, special characters, such as
+ +&
and +?
, for example, will be converted to their hexcode +equivalent. Using the [NE] flag prevents that from happening. ++ +
+RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R] +
+The above example will redirect
+ + + +/anchor/xyz
to +/bigpage.html#xyz
. Omitting the [NE] will result in the # +being converted to its hexcode equivalent,%23
, which will +then result in a 404 Not Found error condition. +NS|nosubreq
+Use of the [NS] flag prevents the rule from being used on +subrequests. For example, a page which is included using an SSI (Server +Side Include) is a subrequest, and you may want to avoid rewrites +happening on those subrequests.
+ ++Images, javascript files, or css files, loaded as part of an HTML page, +are not subrequests - the browser requests them as separate HTTP +requests. +
+ + +P|proxy
+Use of the [P] flag causes the request to be handled by +
+ +mod_proxy
, and handled via a proxy request. For +example, if you wanted all image requests to be handled by a back-end +image server, you might do something like the following:+ +
+RewriteRule (.*)\.(jpg|gif|png) http://images.example.com$1.$2 [P] +
Use of the [P] flag implies [L] - that is, the request is immediatly +pushed through the proxy, and any following rules will not be +considered.
+ + + +PT|passthrough
+ ++The target (or substitution string) in a RewriteRule is assumed to be a +file path, by default. The use of the [PT] flag causes it to be trated +as a URI instead. That is to say, the +use of the [PT] flag causes the result of the
+ +RewriteRule
to be passed back through +URL mapping, so that location-based mappings, such asAlias
, for example, might have a chance to take +effect. ++If, for example, you have an +
+ +Alias
+for /icons, and have aRewriteRule
pointing there, you should +use the [PT] flag to ensure that the +Alias
is evaluated. ++ +
+Alias /icons /usr/local/apache/icons
+RewriteRule /pics/(.+)\.jpg /icons/$1.gif [PT] ++Omission of the [PT] flag in this case will cause the Alias to be +ignored, resulting in a 'File not found' error being returned. +
+ + + +QSA|qsappend
++When the replacement URI contains a query string, the default behavior +of
+ +RewriteRule
is to discard +the existing query string, and replace it with the newly generated one. +Using the [QSA] flag causes the query strings to be combined. +Consider the following rule:
+ ++ +
+RewriteRule /pages/(.+) /page.php?page=$1 [QSA] +
With the [QSA] flag, a request for
+ + +/pages/123?one=two
will be +mapped to/page.php?page=123&one=two
. Without the [QSA] +flag, that same request will be mapped to +/page.php?page=123
- that is, the existing query string +will be discarded. +R|redirect
++Use of the [R] flag causes a HTTP redirect to be issued to the browser. +If a fully-qualified URL is specified (that is, including +
+ +http://servername/
) then a redirect will be issued to that +location. Otherwise, the current servername will be used to generate the +URL sent with the redirect. ++A status code may be specified, in the range 300-399, with a 302 status +code being used by default if none is specified. +
+ ++You will almost always want to use [R] in conjunction with [L] (that is, +use [R,L]) because on its own, the [R] flag prepends +
+ + + +http://thishost[:thisport]
to the URI, but then passes this +on to the next rule in the ruleset, which can often result in 'Invalid +URI in request' warnings. +S|skip
+The [S] flag is used to skip rules that you don't want to run. This +can be thought of as a
+ +goto
statement in your rewrite +ruleset. In the following example, we only want to run theRewriteRule
if the requested URI +doesn't correspond with an actual file.+ +
+# Is the request for a non-existent file? +RewriteCond %{REQUEST_FILENAME} !-f
+RewriteCond %{REQUEST_FILENAME} !-d
+# If so, skip these two RewriteRules +RewriteRule .? - [S=2]
+
+RewriteRule (.*\.gif) images.php?$1
+RewriteRule (.*\.html) docs.php?$1 +This technique is useful because a
+ + + +RewriteCond
only applies to the +RewriteRule
immediately +following it. Thus, if you want to make aRewriteCond
apply +to severalRewriteRule
s, one possible technique is to +negate those conditions and use a [Skip] flag.T|type
+Sets the MIME type with which the resulting response will be +sent. This has the same effect as the
+ +AddType
directive.For example, you might use the following technique to serve Perl +source code as plain text, if requested in a particular way:
+ ++ +
+# Serve .pl files as plan text +RewriteRule \.pl$ - [T=text/plain] +
Or, perhaps, if you have a camera that produces jpeg images without +file extensions, you could force those images to be served with the +correct MIME type by virtue of their file names:
+ ++ +
+# Files with 'IMG' in the name are jpg images.
+RewriteRule IMG - [T=image/jpg] +Please note that this is a trivial example, and could be better done +using
+ + +<FilesMatch>
+instead. Always consider the alternate +solutions to a problem before resorting to rewrite, which will +invariably be a less efficient solution than the alternatives.++ \ No newline at end of file diff --git a/docs/manual/rewrite/rewrite_flags.xml b/docs/manual/rewrite/rewrite_flags.xml new file mode 100644 index 00000000000..1a47cc215c0 --- /dev/null +++ b/docs/manual/rewrite/rewrite_flags.xml @@ -0,0 +1,433 @@ + + + + + + + +Available Languages:
++ + diff --git a/docs/manual/rewrite/rewrite_flags.xml.meta b/docs/manual/rewrite/rewrite_flags.xml.meta new file mode 100644 index 00000000000..7f4813680d8 --- /dev/null +++ b/docs/manual/rewrite/rewrite_flags.xml.meta @@ -0,0 +1,11 @@ + + ++ + Apache mod_rewrite Flags + ++ + +This document discusses the flags which are available to the +
+RewriteRule directive, +providing detailed explanations and examples. This is not necessarily +a comprehensive list of all flags available, so be sure to also +consult the reference documentation.Module documentation +Technical details +Rewrite Guide - useful examples +Advanced Rewrite Guide - +advanced useful examples + ++ + Introduction ++
RewriteRule s can have +their behavior modified by one or more flags. Flags are included in +square brackets at the end of the rule, and multiple flags are separated +by commas.+RewriteRule pattern target [Flag1,Flag2,Flag3] + + +The flags all have a short form, such as
+ +CO
, as well as +a longer form, such ascookie
. Some flags take one or more +arguments. Flags are not case sensitive.+ The flags + +Each flag has a long and short form. While it is most common to use +the short form, it is recommended that you familiarize yourself with the +long form, so that you remember what each flag is supposed to do.
+ +Presented here are each of the available flags, along with an example +of how you might use them.
+ ++ + C|chain +The [C] or [chain] flag indicates that the
+ +RewriteRule is chained to the next +rule. That is, if the rule matches, then it is processed as usual and +control moves on to the next rule. However, if it does not match, then +the next rule, and any other rules that are chained together, will be +skipped.+ + CO|cookie +The [CO], or [cookie] flag, allows you to set a cookie when a +particular
+RewriteRule +matches. The argument consists of three required fields and two optional +fields.You must declare a name and value for the cookie to be set, and the +domain for which you wish the cookie to be valid. You may optionally set +the lifetime of the cookie, and the path for which it should be +returned.
+By default, the lifetime of the cookie is the current browser +session.
+By default, the path for which the cookie will be valid is "/" - that +is, the entire website.
+Several examples are offered here:
+ ++RewriteEngine On + +
+RewriteRule ^/index.html - [CO=frontdoor:yes:.apache.org:1440:/] +This rule doesn't rewrite the request (the "-" rewrite target tells +mod_rewrite to pass the request through unchanged) but sets a cookie +called 'frontdoor' to a value of 'yes'. The cookie is valid for any host +in the
+ +.apache.org
domain. It will be set to expire in 1440 +minutes (24 hours) and will be returned for all URIs.+ + E|env +With the [E], or [env] flag, you can set the value of an environment +variable. Note that some environment variables may be set after the rule +is run, thus unsetting what you have set. See the +Environment Variables document for more details on how Environment +variables work.
+ +The following example sets an evironment variable called 'image' to a +value of '1' if the requested URI is an image file. Then, that +environment variable is used to exclude those requests from the access +log.
+ ++RewriteRule \.(png|gif|jpg) - [E=image:1] + +
+CustomLog logs/access_log combined env=!image +Note that this same effect can be obtained using
+SetEnvIf . This technique is offered as +an example, not as a recommendation.+ + F|forbidden +Using the [F] flag causes Apache to return a 403 Forbidden status +code to the client. While the same behavior can be accomplished using +the
+ +Deny directive, this +allows more flexibility in assigning a Forbidden status.The following rule will forbid
+ +.exe
files from being +downloaded from your server.+RewriteRule \.exe - [F] + + +This example uses the "-" syntax for the rewrite target, which means +that the requested URI is not modified. There's no reason to rewrite to +another URI, if you're going to forbid the request.
+ ++ + G|gone +The [G] flag forces Apache to return a 410 Gone status with the +response. This indicates that a resource used to be available, but is no +longer available.
+ +As with the [F] flag, you will typically use the "-" syntax for the +rewrite target when using the [G] flag:
+ ++RewriteRule oldproduct - [G,NC] + ++ + H|handler +Forces the resulting request to be handled with the specified +handler. For example, one might use this to force all files without a +file extension to be parsed by the php handler:
+ ++RewriteRule !\. - [H=application/x-httpd-php] + + ++The regular expression above -
+!\.
- will match any request +that does not contain the literal.
character. ++ + L|last +The [L] flag causes
+ +mod_rewrite to stop processing +the rule set. In most contexts, this means that if the rule matches, no +further rules will be processed.If you are using
+ +RewriteRule in either +.htaccess
files or in +Directory sections, +it is important to have some understanding of how the rules are +processed. The simplified form of this is that once the rules have been +processed, the rewritten request is handed back to the URL parsing +engine to do what it may with it. It is possible that as the rewritten +request is handled, the.htaccess
file or +Directory section +may be encountered again, and thus the ruleset may be run again from the +start. Most commonly this will happen if one of the rules causes a +redirect - either internal or external - causing the request process to +start over.It is therefore important, if you are using
+ +RewriteRule directives in one of these +context that you take explicit steps to avoid rules looping, and not +count solely on the [L] flag to terminate execution of a series of +rules, as shown below.The example given here will rewrite any request to +
+ +index.php
, giving the original request as a query string +argument toindex.php
, however, if the request is already +forindex.php
, this rull will be skipped.+RewriteCond %{REQUEST_URI} !index\.php +
+RewriteRule ^(.*) index.php?req=$1 [L] ++ + N|next ++The [N] flag causes the ruleset to start over again from the top. Use +with extreme caution, as it may result in loop. +
++The [Next] flag could be used, for example, if you wished to replace a +certain string or letter repeatedly in a request. The example shown here +will replace A with B everywhere in a request, and will continue doing +so until there are no more As to be replaced. +
+ ++RewriteRule (.*)A(.*) $1B$2 [N] + + +You can think of this as a
+ +while
loop: While this +pattern still matches, perform this substitution.+ + NC|nocase +Use of the [NC] flag causes the
+ +RewriteRule to be matched in a +case-insensitive manner. That is, it doesn't care whether letters appear +as upper-case or lower-case in the matched URI.In the example below, any request for an image file will be proxied +to your dedicated image server. The match is case-insensitive, so that +
+ +.jpg
and.JPG
files are both acceptable, for +example.+RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC] + ++ + NE|noescape +By default, special characters, such as
+ +&
and +?
, for example, will be converted to their hexcode +equivalent. Using the [NE] flag prevents that from happening. ++RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R] + + ++The above example will redirect
+ +/anchor/xyz
to +/bigpage.html#xyz
. Omitting the [NE] will result in the # +being converted to its hexcode equivalent,%23
, which will +then result in a 404 Not Found error condition. ++ + NS|nosubreq +Use of the [NS] flag prevents the rule from being used on +subrequests. For example, a page which is included using an SSI (Server +Side Include) is a subrequest, and you may want to avoid rewrites +happening on those subrequests.
+ ++Images, javascript files, or css files, loaded as part of an HTML page, +are not subrequests - the browser requests them as separate HTTP +requests. +
++ + P|proxy +Use of the [P] flag causes the request to be handled by +
+ +mod_proxy , and handled via a proxy request. For +example, if you wanted all image requests to be handled by a back-end +image server, you might do something like the following:+RewriteRule (.*)\.(jpg|gif|png) http://images.example.com$1.$2 [P] + + +Use of the [P] flag implies [L] - that is, the request is immediatly +pushed through the proxy, and any following rules will not be +considered.
+ ++ + PT|passthrough + ++The target (or substitution string) in a RewriteRule is assumed to be a +file path, by default. The use of the [PT] flag causes it to be trated +as a URI instead. That is to say, the +use of the [PT] flag causes the result of the
+ +RewriteRule to be passed back through +URL mapping, so that location-based mappings, such asAlias , for example, might have a chance to take +effect. ++If, for example, you have an +
+ +Alias +for /icons, and have aRewriteRule pointing there, you should +use the [PT] flag to ensure that the +Alias is evaluated. ++Alias /icons /usr/local/apache/icons + +
+RewriteRule /pics/(.+)\.jpg /icons/$1.gif [PT] ++Omission of the [PT] flag in this case will cause the Alias to be +ignored, resulting in a 'File not found' error being returned. +
+ ++ + QSA|qsappend ++When the replacement URI contains a query string, the default behavior +of
+ +RewriteRule is to discard +the existing query string, and replace it with the newly generated one. +Using the [QSA] flag causes the query strings to be combined. +Consider the following rule:
+ ++RewriteRule /pages/(.+) /page.php?page=$1 [QSA] + + +With the [QSA] flag, a request for
+/pages/123?one=two
will be +mapped to/page.php?page=123&one=two
. Without the [QSA] +flag, that same request will be mapped to +/page.php?page=123
- that is, the existing query string +will be discarded. ++ + R|redirect ++Use of the [R] flag causes a HTTP redirect to be issued to the browser. +If a fully-qualified URL is specified (that is, including +
+ +http://servername/
) then a redirect will be issued to that +location. Otherwise, the current servername will be used to generate the +URL sent with the redirect. ++A status code may be specified, in the range 300-399, with a 302 status +code being used by default if none is specified. +
+ ++You will almost always want to use [R] in conjunction with [L] (that is, +use [R,L]) because on its own, the [R] flag prepends +
+ +http://thishost[:thisport]
to the URI, but then passes this +on to the next rule in the ruleset, which can often result in 'Invalid +URI in request' warnings. ++ + S|skip +The [S] flag is used to skip rules that you don't want to run. This +can be thought of as a
+ +goto
statement in your rewrite +ruleset. In the following example, we only want to run theRewriteRule if the requested URI +doesn't correspond with an actual file.+# Is the request for a non-existent file? +RewriteCond %{REQUEST_FILENAME} !-f + +
+RewriteCond %{REQUEST_FILENAME} !-d
+# If so, skip these two RewriteRules +RewriteRule .? - [S=2]
+
+RewriteRule (.*\.gif) images.php?$1
+RewriteRule (.*\.html) docs.php?$1 +This technique is useful because a
+ +RewriteCond only applies to the +RewriteRule immediately +following it. Thus, if you want to make aRewriteCond
apply +to severalRewriteRule
s, one possible technique is to +negate those conditions and use a [Skip] flag.+ + T|type +Sets the MIME type with which the resulting response will be +sent. This has the same effect as the
+ +AddType directive.For example, you might use the following technique to serve Perl +source code as plain text, if requested in a particular way:
+ ++# Serve .pl files as plan text +RewriteRule \.pl$ - [T=text/plain] + + +Or, perhaps, if you have a camera that produces jpeg images without +file extensions, you could force those images to be served with the +correct MIME type by virtue of their file names:
+ ++# Files with 'IMG' in the name are jpg images. + +
+RewriteRule IMG - [T=image/jpg] +Please note that this is a trivial example, and could be better done +using
+FilesMatch +instead. Always consider the alternate +solutions to a problem before resorting to rewrite, which will +invariably be a less efficient solution than the alternatives.+ diff --git a/docs/manual/rewrite/rewrite_guide.html.en b/docs/manual/rewrite/rewrite_guide.html.en index 77e8187a975..4fba8c5b2de 100644 --- a/docs/manual/rewrite/rewrite_guide.html.en +++ b/docs/manual/rewrite/rewrite_guide.html.en @@ -18,8 +18,7 @@rewrite_flags +/rewrite/ +.. + ++ +en ++introductionAdvanced Rewrite Guide - advanced +useful examples Technical details Canonical URLs
@@ -281,12 +280,12 @@ RewriteRule ^/~(.+) http://newserver/~$1 [R,L]@@ -770,8 +770,7 @@ while (<>) {RewriteEngine on -# first try to find it in custom/... +# first try to find it in dir1/... # ...and if found stop and be happy: RewriteCond /your/docroot/dir1/%{REQUEST_FILENAME} -f RewriteRule ^(.+) /your/docroot/dir1/$1 [L] -# second try to find it in pub/... +# second try to find it in dir2/... # ...and if found stop and be happy: RewriteCond /your/docroot/dir2/%{REQUEST_FILENAME} -f RewriteRule ^(.+) /your/docroot/dir2/$1 [L] @@ -588,7 +587,8 @@ RewriteRule ^foo\.html$ bar.html [We just rewrite the URL to the CGI-script and force the - correct MIME-type so it gets really run as a CGI-script. + handler to be cgi-script so that it is + executed as a CGI program. This way a request to
@@ -596,7 +596,7 @@ RewriteRule ^foo\.html$ bar.html [/~quux/foo.html
internally leads to the invocation of/~quux/foo.cgi
.RewriteEngine on RewriteBase /~quux/ -RewriteRule ^foo\.html$ foo.cgi [T=application/x-httpd-cgi] +RewriteRule ^foo\.html$ foo.cgi [H=cgi-script]
RewriteEngine on -# first try to find it in custom/... +# first try to find it in dir1/... # ...and if found stop and be happy: RewriteCond /your/docroot/dir1/%{REQUEST_FILENAME} -f RewriteRule ^(.+) /your/docroot/dir1/$1 [L] -# second try to find it in pub/... +# second try to find it in dir2/... # ...and if found stop and be happy: RewriteCond /your/docroot/dir2/%{REQUEST_FILENAME} -f RewriteRule ^(.+) /your/docroot/dir2/$1 [L] @@ -578,7 +578,8 @@ RewriteRule ^foo\.html$ bar.html [We just rewrite the URL to the CGI-script and force the - correct MIME-type so it gets really run as a CGI-script. + handler to be cgi-script so that it is + executed as a CGI program. This way a request to
@@ -586,7 +587,7 @@ RewriteRule ^foo\.html$ bar.html [/~quux/foo.html
internally leads to the invocation of/~quux/foo.cgi
.RewriteEngine on RewriteBase /~quux/ -RewriteRule ^foo\.html$ foo.cgi [T=application/x-httpd-cgi] +RewriteRule ^foo\.html$ foo.cgi [H=cgi-script]
This document attempts to give sufficient background so that what follows is understood, rather than just copied blindly.
+ +Remember that many common URL-manipulation tasks don't require the
+full power and complexity of mod_rewrite
. For simple
+tasks, see mod_alias
and the documentation
+on mapping URLs to the
+filesystem.
Finally, before proceding, be sure to configure
+the RewriteLog
. Although
+this log file can give an overwhelming amount of information, it is
+indispensable in debugging problems with mod_rewrite
+configuration, since it will tell you exactly how each rule is
+processed.
-Basic anatomy of a RewriteRule, with exhaustively annotated simple -examples. -
+A RewriteRule
consists
+of three arguments separated by spaces. The arguments are
The Pattern is always a regular +expression matched against the URL-Path of the incoming request +(the part after the hostname but before any question mark indicating +the beginning of a query string).
+ +The Substitution can itself be one of three things:
+ +
+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
+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
+for http://example.com/product/r14df/view
will be mapped
+to the path /var/web/productdbr14df
.
If there is more than one expression in parenthesis, they are
+available in order in the
+variables $1
, $2
, $3
, and so
+on.
Discussion of the flags to RewriteRule, and when and why one might -use them.
+The behavior of a RewriteRule
can be modified by the
+application of one 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.
+Discussion of RewriteCond, looping, and other related concepts. -
+One or more RewriteCond
+directives can be used to restrict the types of requests that will be
+subject to the
+following RewriteRule
. The
+first argument is a variable describing a characteristic of the
+request, the second argument is a regular
+expression that must match the variable, and a third optional
+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.
+RewriteRule (.*) http://intranet.example.com$1
+
When more than
+one RewriteCond
is
+specified, they must all match for
+the RewriteRule
to be
+applied. For example, to deny requests that contain the word "hack" in
+their query string, except if they also contain a cookie containing
+the word "go", you could use:
+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
+the RewriteCond
s can be
+used as part of the Substitution in
+the RewriteRule
using the
+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} (.*)
+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
.
Discussion of RewriteMap, including simple, but heavily annotated, -examples.
+See RewriteMap
.
Discussion of the differences between rewrite rules in httpd.conf and -in .htaccess files.
-This module keeps track of two additional (non-standard)
-CGI/SSI environment variables named SCRIPT_URL
-and SCRIPT_URI
. These contain the
-logical Web-view to the current resource, while the
-standard CGI/SSI variables SCRIPT_NAME
and
-SCRIPT_FILENAME
contain the physical
-System-view.
Notice: These variables hold the URI/URL as they were -initially requested, i.e., before any -rewriting. This is important because the rewriting process is -primarily used to rewrite logical URLs to physical -pathnames.
- --SCRIPT_NAME=/sw/lib/w3s/tree/global/u/rse/.www/index.html -SCRIPT_FILENAME=/u/rse/.www/index.html -SCRIPT_URL=/u/rse/ -SCRIPT_URI=http://en1.engelschall.com/u/rse/ -
Rewriting is typically configured in the main server configuration
+setting (outside any <Directory>
section) or
+inside <VirtualHost>
+containers. This is the easiest way to do rewriting and is
+recommended. It is possible, however, to do rewriting
+inside <Directory>
+sections or .htaccess
+files at the expense of some additional complexity. This technique
+is called per-directory rewrites.
The main difference with per-directory rewrites is that the path
+prefix of the directory containing the .htaccess
file is
+stripped before matching in
+the RewriteRule
. In addition, the RewriteBase
should be used to assure the request is properly mapped.
This document attempts to give sufficient background so that what follows is understood, rather than just copied blindly.
+ +Remember that many common URL-manipulation tasks don't require the
+full power and complexity of
Finally, before proceding, be sure to configure
+the
-Basic anatomy of a RewriteRule, with exhaustively annotated simple -examples. -
+A
The Pattern is always a regular +expression matched against the URL-Path of the incoming request +(the part after the hostname but before any question mark indicating +the beginning of a query string).
+ +The Substitution can itself be one of three things:
+ +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
+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
+for http://example.com/product/r14df/view
will be mapped
+to the path /var/web/productdbr14df
.
If there is more than one expression in parenthesis, they are
+available in order in the
+variables $1
, $2
, $3
, and so
+on.
Discussion of the flags to RewriteRule, and when and why one might -use them.
+The behavior of a [NC]
flag:
+
For more details on the available flags, their meanings, and +examples, see the Rewrite Flags document.
+Discussion of RewriteCond, looping, and other related concepts. -
+One or more
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
+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
.
Discussion of RewriteMap, including simple, but heavily annotated, -examples.
+See
Discussion of the differences between rewrite rules in httpd.conf and -in .htaccess files.
-This module keeps track of two additional (non-standard)
-CGI/SSI environment variables named SCRIPT_URL
-and SCRIPT_URI
. These contain the
-logical Web-view to the current resource, while the
-standard CGI/SSI variables SCRIPT_NAME
and
-SCRIPT_FILENAME
contain the physical
-System-view.
Notice: These variables hold the URI/URL as they were -initially requested, i.e., before any -rewriting. This is important because the rewriting process is -primarily used to rewrite logical URLs to physical -pathnames.
- --SCRIPT_NAME=/sw/lib/w3s/tree/global/u/rse/.www/index.html -SCRIPT_FILENAME=/u/rse/.www/index.html -SCRIPT_URL=/u/rse/ -SCRIPT_URI=http://en1.engelschall.com/u/rse/ --
Rewriting is typically configured in the main server configuration
+setting (outside any .htaccess
+files at the expense of some additional complexity. This technique
+is called per-directory rewrites.
The main difference with per-directory rewrites is that the path
+prefix of the directory containing the .htaccess
file is
+stripped before matching in
+the