Blame docs/manual/rewrite/flags.html.en

Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
Packit 90a5c9
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" />
Packit 90a5c9
Packit 90a5c9
        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Packit 90a5c9
              This file is generated from xml source: DO NOT EDIT
Packit 90a5c9
        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Packit 90a5c9
      -->
Packit 90a5c9
<title>RewriteRule Flags - Apache HTTP Server Version 2.4</title>
Packit 90a5c9
<link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
Packit 90a5c9
<link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
Packit 90a5c9
<link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" />
Packit 90a5c9
<script src="../style/scripts/prettify.min.js" type="text/javascript">
Packit 90a5c9
</script>
Packit 90a5c9
Packit 90a5c9
<link href="../images/favicon.ico" rel="shortcut icon" /></head>
Packit 90a5c9
<body id="manual-page">
Packit 90a5c9

Modules | Directives | FAQ | Glossary | Sitemap

Packit 90a5c9

Apache HTTP Server Version 2.4

Packit 90a5c9
Packit 90a5c9
<-
Packit 90a5c9
Packit 90a5c9
Apache > HTTP Server > Documentation > Version 2.4 > Rewrite

RewriteRule Flags

Packit 90a5c9
Packit 90a5c9

Available Languages:  en  |

Packit 90a5c9
 fr 

Packit 90a5c9
Packit 90a5c9
Packit 90a5c9

This document discusses the flags which are available to the

Packit 90a5c9
RewriteRule directive,
Packit 90a5c9
providing detailed explanations and examples.

Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
  • B (escape backreferences)
  • Packit 90a5c9
  • BNP|backrefnoplus (don't escape space to +)
  • Packit 90a5c9
  • C|chain
  • Packit 90a5c9
  • CO|cookie
  • Packit 90a5c9
  • DPI|discardpath
  • Packit 90a5c9
  • E|env
  • Packit 90a5c9
  • END
  • Packit 90a5c9
  • F|forbidden
  • Packit 90a5c9
  • G|gone
  • Packit 90a5c9
  • H|handler
  • Packit 90a5c9
  • L|last
  • Packit 90a5c9
  • N|next
  • Packit 90a5c9
  • NC|nocase
  • Packit 90a5c9
  • NE|noescape
  • Packit 90a5c9
  • NS|nosubreq
  • Packit 90a5c9
  • P|proxy
  • Packit 90a5c9
  • PT|passthrough
  • Packit 90a5c9
  • QSA|qsappend
  • Packit 90a5c9
  • QSD|qsdiscard
  • Packit 90a5c9
  • QSL|qslast
  • Packit 90a5c9
  • R|redirect
  • Packit 90a5c9
  • S|skip
  • Packit 90a5c9
  • T|type
  • Packit 90a5c9

    See also

    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    Introduction

    Packit 90a5c9

    A RewriteRule can have

    Packit 90a5c9
    its behavior modified by one or more flags. Flags are included in
    Packit 90a5c9
    square brackets at the end of the rule, and multiple flags are separated
    Packit 90a5c9
    by commas.

    Packit 90a5c9
    RewriteRule pattern target [Flag1,Flag2,Flag3]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Each flag (with a few exceptions) has a short form, such as

    Packit 90a5c9
    CO, as well as a longer form, such as cookie. 
    Packit 90a5c9
    While it is most common to use
    Packit 90a5c9
    the short form, it is recommended that you familiarize yourself with the
    Packit 90a5c9
    long form, so that you remember what each flag is supposed to do.
    Packit 90a5c9
    Some flags take one or more arguments. Flags are not case sensitive.

    Packit 90a5c9
    Packit 90a5c9

    Flags that alter metadata associated with the request (T=, H=, E=)

    Packit 90a5c9
    have no affect in per-directory and htaccess context, when a substitution
    Packit 90a5c9
    (other than '-') is performed during the same round of rewrite processing.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9

    Presented here are each of the available flags, along with an example

    Packit 90a5c9
    of how you might use them.

    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    B (escape backreferences)

    Packit 90a5c9

    The [B] flag instructs RewriteRule to escape non-alphanumeric

    Packit 90a5c9
    characters before applying the transformation.

    Packit 90a5c9

    In 2.4.26 and later, you can limit the escaping to specific characters

    Packit 90a5c9
    in backreferences by listing them: [B=#?;]. Note: The space
    Packit 90a5c9
    character can be used in the list of characters to escape, but it cannot be
    Packit 90a5c9
    the last character in the list.

    Packit 90a5c9
    Packit 90a5c9

    mod_rewrite has to unescape URLs before mapping them,

    Packit 90a5c9
    so backreferences are unescaped at the time they are applied.
    Packit 90a5c9
    Using the B flag, non-alphanumeric characters in backreferences
    Packit 90a5c9
    will be escaped. For example, consider the rule:

    Packit 90a5c9
    Packit 90a5c9
    RewriteRule "^search/(.*)$" "/search.php?term=$1"
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Given a search term of 'x & y/z', a browser will encode it as

    Packit 90a5c9
    'x%20%26%20y%2Fz', making the request 'search/x%20%26%20y%2Fz'. Without the B
    Packit 90a5c9
    flag, this rewrite rule will map to 'search.php?term=x & y/z', which
    Packit 90a5c9
    isn't a valid URL, and so would be encoded as
    Packit 90a5c9
    search.php?term=x%20&y%2Fz=, which is not what was intended.

    Packit 90a5c9
    Packit 90a5c9

    With the B flag set on this same rule, the parameters are re-encoded

    Packit 90a5c9
    before being passed on to the output URL, resulting in a correct mapping to
    Packit 90a5c9
    /search.php?term=x%20%26%20y%2Fz.

    Packit 90a5c9
    Packit 90a5c9
    RewriteRule "^search/(.*)$" "/search.php?term=$1" [B,PT]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Note that you may also need to set AllowEncodedSlashes to On to get this

    Packit 90a5c9
    particular example to work, as httpd does not allow encoded slashes in URLs, and
    Packit 90a5c9
    returns a 404 if it sees one.

    Packit 90a5c9
    Packit 90a5c9

    This escaping is particularly necessary in a proxy situation,

    Packit 90a5c9
    when the backend may break if presented with an unescaped URL.

    Packit 90a5c9
    Packit 90a5c9

    An alternative to this flag is using a RewriteCond to capture against %{THE_REQUEST} which will capture

    Packit 90a5c9
    strings in the encoded form.

    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    BNP|backrefnoplus (don't escape space to +)

    Packit 90a5c9

    The [BNP] flag instructs RewriteRule to escape the space character

    Packit 90a5c9
    in a backreference to %20 rather than '+'. Useful when the backreference
    Packit 90a5c9
    will be used in the path component rather than the query string.

    Packit 90a5c9
    Packit 90a5c9

    This flag is available in version 2.4.26 and later.

    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    C|chain

    Packit 90a5c9

    The [C] or [chain] flag indicates that the RewriteRule is chained to the next

    Packit 90a5c9
    rule. That is, if the rule matches, then it is processed as usual and
    Packit 90a5c9
    control moves on to the next rule. However, if it does not match, then
    Packit 90a5c9
    the next rule, and any other rules that are chained together, are
    Packit 90a5c9
    skipped.

    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    CO|cookie

    Packit 90a5c9

    The [CO], or [cookie] flag, allows you to set a cookie when a

    Packit 90a5c9
    particular RewriteRule
    Packit 90a5c9
    matches. The argument consists of three required fields and four optional
    Packit 90a5c9
    fields.

    Packit 90a5c9
    Packit 90a5c9

    The full syntax for the flag, including all attributes, is as

    Packit 90a5c9
    follows:

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    [CO=NAME:VALUE:DOMAIN:lifetime:path:secure:httponly]
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9

    If a literal ':' character is needed in any of the cookie fields, an

    Packit 90a5c9
    alternate syntax is available.  To opt-in to the alternate syntax, the cookie 
    Packit 90a5c9
    "Name" should be preceded with a ';' character, and field separators should be
    Packit 90a5c9
    specified as ';'.

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    [CO=;NAME;VALUE:MOREVALUE;DOMAIN;lifetime;path;secure;httponly]
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9

    You must declare a name, a value, and a domain for the cookie to be set.

    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9
    Domain
    Packit 90a5c9
    The domain for which you want the cookie to be valid. This may be a
    Packit 90a5c9
    hostname, such as www.example.com, or it may be a domain,
    Packit 90a5c9
    such as .example.com. It must be at least two parts
    Packit 90a5c9
    separated by a dot. That is, it may not be merely .com or
    Packit 90a5c9
    .net. Cookies of that kind are forbidden by the cookie
    Packit 90a5c9
    security model.
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    You may optionally also set the following values:

    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9
    Lifetime
    Packit 90a5c9
    The time for which the cookie will persist, in minutes.
    Packit 90a5c9
    A value of 0 indicates that the cookie will persist only for the
    Packit 90a5c9
    current browser session. This is the default value if none is
    Packit 90a5c9
    specified.
    Packit 90a5c9
    Packit 90a5c9
    Path
    Packit 90a5c9
    The path, on the current website, for which the cookie is valid,
    Packit 90a5c9
    such as /customers/ or /files/download/.
    Packit 90a5c9
    By default, this is set to / - that is, the entire
    Packit 90a5c9
    website.
    Packit 90a5c9
    Packit 90a5c9
    Secure
    Packit 90a5c9
    If set to secure, true, or 1,
    Packit 90a5c9
    the cookie will only be permitted to be translated via secure (https)
    Packit 90a5c9
    connections.
    Packit 90a5c9
    Packit 90a5c9
    httponly
    Packit 90a5c9
    If set to HttpOnly, true, or
    Packit 90a5c9
    1, the cookie will have the HttpOnly flag set,
    Packit 90a5c9
    which means that the cookie is inaccessible to JavaScript code on
    Packit 90a5c9
    browsers that support this feature.
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Consider this example:

    Packit 90a5c9
    Packit 90a5c9
    RewriteEngine On
    Packit 90a5c9
    RewriteRule "^/index\.html" "-" [CO=frontdoor:yes:.example.com:1440:/]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    In the example give, the rule doesn't rewrite the request.

    Packit 90a5c9
    The "-" rewrite target tells mod_rewrite to pass the request
    Packit 90a5c9
    through unchanged. Instead, it sets a cookie
    Packit 90a5c9
    called 'frontdoor' to a value of 'yes'. The cookie is valid for any host
    Packit 90a5c9
    in the .example.com domain. It is set to expire in 1440
    Packit 90a5c9
    minutes (24 hours) and is returned for all URIs.

    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    DPI|discardpath

    Packit 90a5c9

    The DPI flag causes the PATH_INFO portion of the rewritten URI to be

    Packit 90a5c9
    discarded.

    Packit 90a5c9

    This flag is available in version 2.2.12 and later.

    Packit 90a5c9

    In per-directory context, the URI each RewriteRule

    Packit 90a5c9
    compares against is the concatenation of the current values of the URI
    Packit 90a5c9
    and PATH_INFO.

    Packit 90a5c9
    Packit 90a5c9

    The current URI can be the initial URI as requested by the client, the

    Packit 90a5c9
    result of a previous round of mod_rewrite processing, or the result of
    Packit 90a5c9
    a prior rule in the current round of mod_rewrite processing.

    Packit 90a5c9
    Packit 90a5c9

    In contrast, the PATH_INFO that is appended to the URI before each

    Packit 90a5c9
    rule reflects only the value of PATH_INFO before this round of
    Packit 90a5c9
    mod_rewrite processing. As a consequence, if large portions
    Packit 90a5c9
    of the URI are matched and copied into a substitution in multiple
    Packit 90a5c9
    RewriteRule directives, without regard for
    Packit 90a5c9
    which parts of the URI came from the current PATH_INFO, the final
    Packit 90a5c9
    URI may have multiple copies of PATH_INFO appended to it.

    Packit 90a5c9
    Packit 90a5c9

    Use this flag on any substitution where the PATH_INFO that resulted

    Packit 90a5c9
    from the previous mapping of this request to the filesystem is not of
    Packit 90a5c9
    interest.  This flag permanently forgets the PATH_INFO established
    Packit 90a5c9
    before this round of mod_rewrite processing began. PATH_INFO will
    Packit 90a5c9
    not be recalculated until the current round of mod_rewrite processing
    Packit 90a5c9
    completes.  Subsequent rules during this round of processing will see
    Packit 90a5c9
    only the direct result of substitutions, without any PATH_INFO
    Packit 90a5c9
    appended.

    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    E|env

    Packit 90a5c9

    With the [E], or [env] flag, you can set the value of an environment

    Packit 90a5c9
    variable. Note that some environment variables may be set after the rule
    Packit 90a5c9
    is run, thus unsetting what you have set. See the
    Packit 90a5c9
    Environment Variables document for more details on how Environment
    Packit 90a5c9
    variables work.

    Packit 90a5c9
    Packit 90a5c9

    The full syntax for this flag is:

    Packit 90a5c9
    Packit 90a5c9
    [E=VAR:VAL]
    Packit 90a5c9
    [E=!VAR]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    VAL may contain backreferences ($N or

    Packit 90a5c9
    %N) which are expanded.

    Packit 90a5c9
    Packit 90a5c9

    Using the short form

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    [E=VAR]
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9

    you can set the environment variable named VAR to an

    Packit 90a5c9
    empty value.

    Packit 90a5c9
    Packit 90a5c9

    The form

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    [E=!VAR]
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9

    allows to unset a previously set environment variable named

    Packit 90a5c9
    VAR.

    Packit 90a5c9
    Packit 90a5c9

    Environment variables can then be used in a variety of

    Packit 90a5c9
    contexts, including CGI programs, other RewriteRule directives, or
    Packit 90a5c9
    CustomLog directives.

    Packit 90a5c9
    Packit 90a5c9

    The following example sets an environment variable called 'image' to a

    Packit 90a5c9
    value of '1' if the requested URI is an image file. Then, that
    Packit 90a5c9
    environment variable is used to exclude those requests from the access
    Packit 90a5c9
    log.

    Packit 90a5c9
    Packit 90a5c9
    RewriteRule "\.(png|gif|jpg)$" "-" [E=image:1]
    Packit 90a5c9
    CustomLog "logs/access_log" combined env=!image
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Note that this same effect can be obtained using SetEnvIf. This technique is offered as

    Packit 90a5c9
    an example, not as a recommendation.

    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    END

    Packit 90a5c9

    Using the [END] flag terminates not only the current round of rewrite

    Packit 90a5c9
    processing (like [L]) but also prevents any subsequent rewrite
    Packit 90a5c9
    processing from occurring in per-directory (htaccess) context.

    Packit 90a5c9
    Packit 90a5c9

    This does not apply to new requests resulting from external

    Packit 90a5c9
    redirects.

    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    F|forbidden

    Packit 90a5c9

    Using the [F] flag causes the server to return a 403 Forbidden status

    Packit 90a5c9
    code to the client. While the same behavior can be accomplished using
    Packit 90a5c9
    the Deny directive, this
    Packit 90a5c9
    allows more flexibility in assigning a Forbidden status.

    Packit 90a5c9
    Packit 90a5c9

    The following rule will forbid .exe files from being

    Packit 90a5c9
    downloaded from your server.

    Packit 90a5c9
    Packit 90a5c9
    RewriteRule "\.exe" "-" [F]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    This example uses the "-" syntax for the rewrite target, which means

    Packit 90a5c9
    that the requested URI is not modified. There's no reason to rewrite to
    Packit 90a5c9
    another URI, if you're going to forbid the request.

    Packit 90a5c9
    Packit 90a5c9

    When using [F], an [L] is implied - that is, the response is returned

    Packit 90a5c9
    immediately, and no further rules are evaluated.

    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    G|gone

    Packit 90a5c9

    The [G] flag forces the server to return a 410 Gone status with the

    Packit 90a5c9
    response. This indicates that a resource used to be available, but is no
    Packit 90a5c9
    longer available.

    Packit 90a5c9
    Packit 90a5c9

    As with the [F] flag, you will typically use the "-" syntax for the

    Packit 90a5c9
    rewrite target when using the [G] flag:

    Packit 90a5c9
    Packit 90a5c9
    RewriteRule "oldproduct" "-" [G,NC]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    When using [G], an [L] is implied - that is, the response is returned

    Packit 90a5c9
    immediately, and no further rules are evaluated.

    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    H|handler

    Packit 90a5c9

    Forces the resulting request to be handled with the specified

    Packit 90a5c9
    handler. For example, one might use this to force all files without a
    Packit 90a5c9
    file extension to be parsed by the php handler:

    Packit 90a5c9
    Packit 90a5c9
    RewriteRule "!\." "-" [H=application/x-httpd-php]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    The regular expression above - !\. - will match any request
    Packit 90a5c9
    that does not contain the literal . character.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9

    This can be also used to force the handler based on some conditions.

    Packit 90a5c9
    For example, the following snippet used in per-server context allows
    Packit 90a5c9
    .php files to be displayed by mod_php
    Packit 90a5c9
    if they are requested with the .phps extension:

    Packit 90a5c9
    Packit 90a5c9
    RewriteRule "^(/source/.+\.php)s$" "$1" [H=application/x-httpd-php-source]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    The regular expression above - ^(/source/.+\.php)s$ - will

    Packit 90a5c9
    match any request that starts with /source/ followed by 1 or
    Packit 90a5c9
    n characters followed by .phps literally. The backreference
    Packit 90a5c9
    $1 referrers to the captured match within parenthesis of the regular
    Packit 90a5c9
    expression.

    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    L|last

    Packit 90a5c9

    The [L] flag causes mod_rewrite to stop processing

    Packit 90a5c9
    the rule set. In most contexts, this means that if the rule matches, no
    Packit 90a5c9
    further rules will be processed. This corresponds to the
    Packit 90a5c9
    last command in Perl, or the break command in
    Packit 90a5c9
    C. Use this flag to indicate that the current rule should be applied
    Packit 90a5c9
    immediately without considering further rules.

    Packit 90a5c9
    Packit 90a5c9

    If you are using RewriteRule in either

    Packit 90a5c9
    .htaccess files or in
    Packit 90a5c9
    <Directory> sections,
    Packit 90a5c9
    it is important to have some understanding of how the rules are
    Packit 90a5c9
    processed.  The simplified form of this is that once the rules have been
    Packit 90a5c9
    processed, the rewritten request is handed back to the URL parsing
    Packit 90a5c9
    engine to do what it may with it. It is possible that as the rewritten
    Packit 90a5c9
    request is handled, the .htaccess file or
    Packit 90a5c9
    <Directory> section
    Packit 90a5c9
    may be encountered again, and thus the ruleset may be run again from the
    Packit 90a5c9
    start. Most commonly this will happen if one of the rules causes a
    Packit 90a5c9
    redirect - either internal or external - causing the request process to
    Packit 90a5c9
    start over.

    Packit 90a5c9
    Packit 90a5c9

    It is therefore important, if you are using RewriteRule directives in one of these

    Packit 90a5c9
    contexts, that you take explicit steps to avoid rules looping, and not
    Packit 90a5c9
    count solely on the [L] flag to terminate execution of a series of
    Packit 90a5c9
    rules, as shown below.

    Packit 90a5c9
    Packit 90a5c9

    An alternative flag, [END], can be used to terminate not only the

    Packit 90a5c9
    current round of rewrite processing but prevent any subsequent
    Packit 90a5c9
    rewrite processing from occurring in per-directory (htaccess)
    Packit 90a5c9
    context. This does not apply to new requests resulting from external
    Packit 90a5c9
    redirects.

    Packit 90a5c9
    Packit 90a5c9

    The example given here will rewrite any request to

    Packit 90a5c9
    index.php, giving the original request as a query string
    Packit 90a5c9
    argument to index.php, however, the RewriteCond ensures that if the request
    Packit 90a5c9
    is already for index.php, the RewriteRule will be skipped.

    Packit 90a5c9
    Packit 90a5c9
    RewriteBase "/"
    Packit 90a5c9
    RewriteCond "%{REQUEST_URI}" "!=/index.php"
    Packit 90a5c9
    RewriteRule "^(.*)" "/index.php?req=$1" [L,PT]
    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    N|next

    Packit 90a5c9

    Packit 90a5c9
    The [N] flag causes the ruleset to start over again from the top, using
    Packit 90a5c9
    the result of the ruleset so far as a starting point. Use
    Packit 90a5c9
    with extreme caution, as it may result in loop.
    Packit 90a5c9

    Packit 90a5c9

    Packit 90a5c9
    The [Next] flag could be used, for example, if you wished to replace a
    Packit 90a5c9
    certain string or letter repeatedly in a request. The example shown here
    Packit 90a5c9
    will replace A with B everywhere in a request, and will continue doing
    Packit 90a5c9
    so until there are no more As to be replaced.
    Packit 90a5c9

    Packit 90a5c9
    RewriteRule "(.*)A(.*)" "$1B$2" [N]
    Packit 90a5c9
    Packit 90a5c9

    You can think of this as a while loop: While this

    Packit 90a5c9
    pattern still matches (i.e., while the URI still contains an
    Packit 90a5c9
    A), perform this substitution (i.e., replace the
    Packit 90a5c9
    A with a B).

    Packit 90a5c9
    Packit 90a5c9

    In 2.4.8 and later, this module returns an error after 32,000 iterations to

    Packit 90a5c9
    protect against unintended looping.  An alternative maximum number of 
    Packit 90a5c9
    iterations can be specified by adding to the N flag.  

    Packit 90a5c9
    # Be willing to replace 1 character in each pass of the loop
    Packit 90a5c9
    RewriteRule "(.+)[><;]$" "$1" [N=64000]
    Packit 90a5c9
    # ... or, give up if after 10 loops
    Packit 90a5c9
    RewriteRule "(.+)[><;]$" "$1" [N=10]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    NC|nocase

    Packit 90a5c9

    Use of the [NC] flag causes the RewriteRule to be matched in a

    Packit 90a5c9
    case-insensitive manner. That is, it doesn't care whether letters appear
    Packit 90a5c9
    as upper-case or lower-case in the matched URI.

    Packit 90a5c9
    Packit 90a5c9

    In the example below, any request for an image file will be proxied

    Packit 90a5c9
    to your dedicated image server. The match is case-insensitive, so that
    Packit 90a5c9
    .jpg and .JPG files are both acceptable, for
    Packit 90a5c9
    example.

    Packit 90a5c9
    Packit 90a5c9
    RewriteRule "(.*\.(jpg|gif|png))$" "http://images.example.com$1" [P,NC]
    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    NE|noescape

    Packit 90a5c9

    By default, special characters, such as & and

    Packit 90a5c9
    ?, for example, will be converted to their hexcode
    Packit 90a5c9
    equivalent. Using the [NE] flag prevents that from happening.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9
    RewriteRule "^/anchor/(.+)" "/bigpage.html#$1" [NE,R]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    The above example will redirect /anchor/xyz to
    Packit 90a5c9
    /bigpage.html#xyz. Omitting the [NE] will result in the #
    Packit 90a5c9
    being converted to its hexcode equivalent, %23, which will
    Packit 90a5c9
    then result in a 404 Not Found error condition.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    NS|nosubreq

    Packit 90a5c9

    Use of the [NS] flag prevents the rule from being used on

    Packit 90a5c9
    subrequests. For example, a page which is included using an SSI (Server
    Packit 90a5c9
    Side Include) is a subrequest, and you may want to avoid rewrites
    Packit 90a5c9
    happening on those subrequests. Also, when mod_dir
    Packit 90a5c9
    tries to find out information about possible directory default files
    Packit 90a5c9
    (such as index.html files), this is an internal
    Packit 90a5c9
    subrequest, and you often want to avoid rewrites on such subrequests.
    Packit 90a5c9
    On subrequests, it is not always useful, and can even cause errors, if
    Packit 90a5c9
    the complete set of rules are applied. Use this flag to exclude
    Packit 90a5c9
    problematic rules.

    Packit 90a5c9
    Packit 90a5c9

    To decide whether or not to use this rule: if you prefix URLs with

    Packit 90a5c9
    CGI-scripts, to force them to be processed by the CGI-script, it's
    Packit 90a5c9
    likely that you will run into problems (or significant overhead)
    Packit 90a5c9
    on sub-requests. In these cases, use this flag.

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    Images, javascript files, or css files, loaded as part of an HTML page,
    Packit 90a5c9
    are not subrequests - the browser requests them as separate HTTP
    Packit 90a5c9
    requests.
    Packit 90a5c9

    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    P|proxy

    Packit 90a5c9

    Use of the [P] flag causes the request to be handled by

    Packit 90a5c9
    mod_proxy, and handled via a proxy request. For
    Packit 90a5c9
    example, if you wanted all image requests to be handled by a back-end
    Packit 90a5c9
    image server, you might do something like the following:

    Packit 90a5c9
    Packit 90a5c9
    RewriteRule "/(.*)\.(jpg|gif|png)$" "http://images.example.com/$1.$2" [P]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Use of the [P] flag implies [L] - that is, the request is immediately

    Packit 90a5c9
    pushed through the proxy, and any following rules will not be
    Packit 90a5c9
    considered.

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    You must make sure that the substitution string is a valid URI
    Packit 90a5c9
    (typically starting with http://hostname) which can be
    Packit 90a5c9
    handled by the mod_proxy. If not, you will get an
    Packit 90a5c9
    error from the proxy module. Use this flag to achieve a
    Packit 90a5c9
    more powerful implementation of the ProxyPass directive,
    Packit 90a5c9
    to map remote content into the namespace of the local server.

    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Security Warning

    Packit 90a5c9

    Take care when constructing the target URL of the rule, considering

    Packit 90a5c9
    the security impact from allowing the client influence over the set of
    Packit 90a5c9
    URLs to which your server will act as a proxy.  Ensure that the scheme
    Packit 90a5c9
    and hostname part of the URL is either fixed, or does not allow the
    Packit 90a5c9
    client undue influence.

    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Performance warning

    Packit 90a5c9

    Using this flag triggers the use of mod_proxy, without handling of persistent connections. This

    Packit 90a5c9
    means the performance of your proxy will be better if you set it up with ProxyPass or
    Packit 90a5c9
    ProxyPassMatch

    Packit 90a5c9

    This is because this flag triggers the use of the default worker, which does not handle connection pooling/reuse.

    Packit 90a5c9

    Avoid using this flag and prefer those directives, whenever you can.

    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Note: mod_proxy must be enabled in order

    Packit 90a5c9
    to use this flag.

    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    PT|passthrough

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    The target (or substitution string) in a RewriteRule is assumed to be a
    Packit 90a5c9
    file path, by default. The use of the [PT] flag causes it to be treated
    Packit 90a5c9
    as a URI instead. That is to say, the
    Packit 90a5c9
    use of the [PT] flag causes the result of the RewriteRule to be passed back through
    Packit 90a5c9
    URL mapping, so that location-based mappings, such as Alias, Redirect, or ScriptAlias, for example, might have a
    Packit 90a5c9
    chance to take effect.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    If, for example, you have an
    Packit 90a5c9
    Alias
    Packit 90a5c9
    for /icons, and have a RewriteRule pointing there, you should
    Packit 90a5c9
    use the [PT] flag to ensure that the
    Packit 90a5c9
    Alias is evaluated.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9
    Alias "/icons" "/usr/local/apache/icons"
    Packit 90a5c9
    RewriteRule "/pics/(.+)\.jpg$" "/icons/$1.gif" [PT]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    Omission of the [PT] flag in this case will cause the Alias to be
    Packit 90a5c9
    ignored, resulting in a 'File not found' error being returned.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9

    The PT flag implies the L flag:

    Packit 90a5c9
    rewriting will be stopped in order to pass the request to
    Packit 90a5c9
    the next phase of processing.

    Packit 90a5c9
    Packit 90a5c9

    Note that the PT flag is implied in per-directory

    Packit 90a5c9
    contexts such as
    Packit 90a5c9
    <Directory> sections
    Packit 90a5c9
    or in .htaccess files. The only way to circumvent that
    Packit 90a5c9
    is to rewrite to -.

    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    QSA|qsappend

    Packit 90a5c9

    Packit 90a5c9
    When the replacement URI contains a query string, the default behavior
    Packit 90a5c9
    of RewriteRule is to discard
    Packit 90a5c9
    the existing query string, and replace it with the newly generated one.
    Packit 90a5c9
    Using the [QSA] flag causes the query strings to be combined.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9

    Consider the following rule:

    Packit 90a5c9
    Packit 90a5c9
    RewriteRule "/pages/(.+)" "/page.php?page=$1" [QSA]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    With the [QSA] flag, a request for /pages/123?one=two will be

    Packit 90a5c9
    mapped to /page.php?page=123&one=two. Without the [QSA]
    Packit 90a5c9
    flag, that same request will be mapped to
    Packit 90a5c9
    /page.php?page=123 - that is, the existing query string
    Packit 90a5c9
    will be discarded.
    Packit 90a5c9

    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    QSD|qsdiscard

    Packit 90a5c9

    Packit 90a5c9
    When the requested URI contains a query string, and the target URI does
    Packit 90a5c9
    not, the default behavior of RewriteRule is to copy that query
    Packit 90a5c9
    string to the target URI. Using the [QSD] flag causes the query string
    Packit 90a5c9
    to be discarded.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9

    This flag is available in version 2.4.0 and later.

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    Using [QSD] and [QSA] together will result in [QSD] taking precedence.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    If the target URI has a query string, the default behavior will be
    Packit 90a5c9
    observed - that is, the original query string will be discarded and
    Packit 90a5c9
    replaced with the query string in the RewriteRule target
    Packit 90a5c9
    URI.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    QSL|qslast

    Packit 90a5c9

    Packit 90a5c9
    By default, the first (left-most) question mark in the substitution
    Packit 90a5c9
    delimits the path from the query string.  Using the [QSL] flag instructs
    Packit 90a5c9
    RewriteRule to instead split
    Packit 90a5c9
    the two components using the last (right-most) question mark.  

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    This is useful when mapping to files that have literal question marks in 
    Packit 90a5c9
    their filename.  If no query string is used in the substitution, 
    Packit 90a5c9
    a question mark can be appended to it in combination with this flag.  

    Packit 90a5c9
    Packit 90a5c9

    This flag is available in version 2.4.19 and later.

    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    R|redirect

    Packit 90a5c9

    Packit 90a5c9
    Use of the [R] flag causes a HTTP redirect to be issued to the browser.
    Packit 90a5c9
    If a fully-qualified URL is specified (that is, including
    Packit 90a5c9
    http://servername/) then a redirect will be issued to that
    Packit 90a5c9
    location. Otherwise, the current protocol, servername, and port number
    Packit 90a5c9
    will be used to generate the URL sent with the redirect.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    Any valid HTTP response  status code may be specified,
    Packit 90a5c9
    using the syntax [R=305], with a 302 status code being used by
    Packit 90a5c9
    default if none is specified. The status code specified need not
    Packit 90a5c9
    necessarily be a redirect (3xx) status code. However,
    Packit 90a5c9
    if a status code is outside the redirect range (300-399) then the
    Packit 90a5c9
    substitution string is dropped entirely, and rewriting is stopped as if
    Packit 90a5c9
    the L were used.

    Packit 90a5c9
    Packit 90a5c9

    In addition to response status codes, you may also specify redirect

    Packit 90a5c9
    status using their symbolic names: temp (default),
    Packit 90a5c9
    permanent, or seeother.

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    You will almost always want to use [R] in conjunction with [L] (that is,
    Packit 90a5c9
    use [R,L]) because on its own, the [R] flag prepends
    Packit 90a5c9
    http://thishost[:thisport] to the URI, but then passes this
    Packit 90a5c9
    on to the next rule in the ruleset, which can often result in 'Invalid
    Packit 90a5c9
    URI in request' warnings.
    Packit 90a5c9

    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    S|skip

    Packit 90a5c9

    The [S] flag is used to skip rules that you don't want to run. The

    Packit 90a5c9
    syntax of the skip flag is [S=N], where N signifies
    Packit 90a5c9
    the number of rules to skip (provided the 
    Packit 90a5c9
    RewriteRule matches). This can be thought of as a goto
    Packit 90a5c9
    statement in your rewrite ruleset. In the following example, we only want
    Packit 90a5c9
    to run the RewriteRule if the
    Packit 90a5c9
    requested URI doesn't correspond with an actual file.

    Packit 90a5c9
    Packit 90a5c9
    # Is the request for a non-existent file?
    Packit 90a5c9
    RewriteCond "%{REQUEST_FILENAME}" "!-f"
    Packit 90a5c9
    RewriteCond "%{REQUEST_FILENAME}" "!-d"
    Packit 90a5c9
    # If so, skip these two RewriteRules
    Packit 90a5c9
    RewriteRule ".?" "-" [S=2]
    Packit 90a5c9
    Packit 90a5c9
    RewriteRule "(.*\.gif)" "images.php?$1"
    Packit 90a5c9
    RewriteRule "(.*\.html)" "docs.php?$1"
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    This technique is useful because a RewriteCond only applies to the

    Packit 90a5c9
    RewriteRule immediately
    Packit 90a5c9
    following it. Thus, if you want to make a RewriteCond apply
    Packit 90a5c9
    to several RewriteRules, one possible technique is to
    Packit 90a5c9
    negate those conditions and add a RewriteRule with a [Skip] flag. You can
    Packit 90a5c9
    use this to make pseudo if-then-else constructs: The last rule of
    Packit 90a5c9
    the then-clause becomes skip=N, where N is the
    Packit 90a5c9
    number of rules in the else-clause:

    Packit 90a5c9
    # Does the file exist?
    Packit 90a5c9
    RewriteCond "%{REQUEST_FILENAME}" "!-f"
    Packit 90a5c9
    RewriteCond "%{REQUEST_FILENAME}" "!-d"
    Packit 90a5c9
    # Create an if-then-else construct by skipping 3 lines if we meant to go to the "else" stanza.
    Packit 90a5c9
    RewriteRule ".?" "-" [S=3]
    Packit 90a5c9
    Packit 90a5c9
    # IF the file exists, then:
    Packit 90a5c9
        RewriteRule "(.*\.gif)" "images.php?$1"
    Packit 90a5c9
        RewriteRule "(.*\.html)" "docs.php?$1"
    Packit 90a5c9
        # Skip past the "else" stanza.
    Packit 90a5c9
        RewriteRule ".?" "-" [S=1]
    Packit 90a5c9
    # ELSE...
    Packit 90a5c9
        RewriteRule "(.*)" "404.php?file=$1"
    Packit 90a5c9
    # END
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    It is probably easier to accomplish this kind of configuration using

    Packit 90a5c9
    the <If>, <ElseIf>, and <Else> directives instead.

    Packit 90a5c9
    Packit 90a5c9
    top
    Packit 90a5c9
    Packit 90a5c9

    T|type

    Packit 90a5c9

    Sets the MIME type with which the resulting response will be

    Packit 90a5c9
    sent. This has the same effect as the AddType directive.

    Packit 90a5c9
    Packit 90a5c9

    For example, you might use the following technique to serve Perl

    Packit 90a5c9
    source code as plain text, if requested in a particular way:

    Packit 90a5c9
    Packit 90a5c9
    # Serve .pl files as plain text
    Packit 90a5c9
    RewriteRule "\.pl$" "-" [T=text/plain]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Or, perhaps, if you have a camera that produces jpeg images without

    Packit 90a5c9
    file extensions, you could force those images to be served with the
    Packit 90a5c9
    correct MIME type by virtue of their file names:

    Packit 90a5c9
    Packit 90a5c9
    # Files with 'IMG' in the name are jpg images.
    Packit 90a5c9
    RewriteRule "IMG" "-" [T=image/jpg]
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Please note that this is a trivial example, and could be better done

    Packit 90a5c9
    using <FilesMatch>
    Packit 90a5c9
    instead. Always consider the alternate
    Packit 90a5c9
    solutions to a problem before resorting to rewrite, which will
    Packit 90a5c9
    invariably be a less efficient solution than the alternatives.

    Packit 90a5c9
    Packit 90a5c9

    Packit 90a5c9
    If used in per-directory context, use only - (dash)
    Packit 90a5c9
    as the substitution for the entire round of mod_rewrite processing,
    Packit 90a5c9
    otherwise the MIME-type set with this flag is lost due to an internal
    Packit 90a5c9
    re-processing (including subsequent rounds of mod_rewrite processing).
    Packit 90a5c9
    The L flag can be useful in this context to end the
    Packit 90a5c9
    current round of mod_rewrite processing.

    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9
    Packit 90a5c9

    Available Languages:  en  |

    Packit 90a5c9
     fr 

    Packit 90a5c9
    top

    Comments

    Notice:
    This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our mailing lists.
    Packit 90a5c9
    <script type="text/javascript">
    Packit 90a5c9
    var comments_shortname = 'httpd';
    Packit 90a5c9
    var comments_identifier = 'http://httpd.apache.org/docs/2.4/rewrite/flags.html';
    Packit 90a5c9
    (function(w, d) {
    Packit 90a5c9
        if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
    Packit 90a5c9
            d.write('
    <\/div>');
    Packit 90a5c9
            var s = d.createElement('script');
    Packit 90a5c9
            s.type = 'text/javascript';
    Packit 90a5c9
            s.async = true;
    Packit 90a5c9
            s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
    Packit 90a5c9
            (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
    Packit 90a5c9
        }
    Packit 90a5c9
        else { 
    Packit 90a5c9
            d.write('
    Comments are disabled for this page at the moment.<\/div>');
    Packit 90a5c9
        }
    Packit 90a5c9
    })(window, document);
    Packit 90a5c9
    //--></script>
    Packit 90a5c9

    Copyright 2018 The Apache Software Foundation.
    Licensed under the Apache License, Version 2.0.

    Packit 90a5c9

    Modules | Directives | FAQ | Glossary | Sitemap

    <script type="text/javascript">
    Packit 90a5c9
    if (typeof(prettyPrint) !== 'undefined') {
    Packit 90a5c9
        prettyPrint();
    Packit 90a5c9
    }
    Packit 90a5c9
    //--></script>
    Packit 90a5c9
    </body></html>