This document discusses the flags which are available to the
The flags all have a short form, such as CO
, as well as
a longer form, such as cookie
. Some flags take one or more
arguments. Flags are not case sensitive.
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.
The [C] or [chain] flag indicates that the
The [CO], or [cookie] flag, allows you to set a cookie when a
particular
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:
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.
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.
Note that this same effect can be obtained using
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
The following rule will forbid .exe
files from being
downloaded from your server.
This example uses the "-" syntax for the rewrite target, which means that the requested URI is not modified.
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.
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:
The regular expression above - !\.
- will match any request
that does not contain the literal .
character.
The [L] flag causes
If you are using .htaccess
files or in
.htaccess
file or
It is therefore important, if you are using
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.
Use of the [NC] flag causes the
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.
By default, special characters, such as &
and
?
, for example, will be converted to their hexcode
equivalent. Using the [NE] flag prevents that from happening.
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
.
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.
Use of the [P] flag causes the request to be handled by
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.
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
If, for example, you have an
Omission of the [PT] flag in this case will cause the Alias to be ignored, resulting in a 'File not found' error being returned.
When the replacement URI contains a query string, the default behavior
of
Consider the following rule:
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.
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.
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 the
This technique is useful because a RewriteCond
apply
to several RewriteRule
s, one possible technique is to
negate those conditions and use a [Skip] flag.
Sets the MIME type with which the resulting response will be
sent. This has the same effect as the
For example, you might use the following technique to serve Perl source code as plain text, if requested in a particular way:
Please note that this is a trivial example, and could be better done
using