Text of httpd.apache.org › docs › current › rewrite › flags.html as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

RewriteRule Flags - Apache HTTP Server Version 2.4

Modules | Directives | FAQ | Glossary | Sitemap | Report a bug Apache HTTP Server Version 2.4 Apache > HTTP Server > Documentation > Version 2.4 > Rewrite RewriteRule Flags Available Languages: en | fr This document discusses the flags which are available to the RewriteRule directive, providing detailed explanations and examples. Introduction Flag Quick Reference B (escape backreferences) BNP|backrefnoplus (don't escape space to +) BCTLS BNE C|chain CO|cookie DPI|discardpath E|env END F|forbidden G|gone H|handler L|last N|next NC|nocase NE|noescape NS|nosubreq P|proxy PT|passthrough QSA|qsappend QSD|qsdiscard QSL|qslast R|redirect S|skip T|type UnsafeAllow3F UnsafePrefixStat UNC See also Module documentation mod_rewrite introduction Redirection and remapping Per-directory Rewrites Virtual hosts Using RewriteMap When not to use mod_rewrite Technical details Introduction ¶ A RewriteRule can have its 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] Each flag (with a few exceptions) has a short form, such as CO , as well as a longer form, such as cookie . 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.

Some flags take one or more arguments. Flags are not case sensitive. Flags that alter metadata associated with the request (T=, H=, E=) have no effect in per-directory context , when a substitution (other than '-') is performed during the same round of rewrite processing. Presented here are each of the available flags, along with an example of how you might use them. Flag Quick Reference ¶ Flags can be combined: [R=301,L] , [P,QSA] , [E=VAR:val,L] .

This table groups them by purpose, ordered by how commonly each is used. Flag Purpose Effect Common combos Flow Control [L] Last rule Stop processing rules (this pass) [R=301,L] [F] [G] [END] Full stop Stop all rewrite processing (no re-entry in .htaccess) [R=301,END] [S=N] Skip Skip next N rules (if/else logic) [N] Next (loop) Restart ruleset from the top (caution: loop risk) [C] Chain Tie rule to the next; if this fails, skip chained rules Redirection and Proxying [R=code] Redirect External redirect (default 302).

Consider Redirect/RedirectMatch for simple cases [R=301,L] [R=302,L] [P] Proxy Reverse proxy to target (requires mod_proxy ) [P,QSA] Access Control [F] Forbidden Return 403 (implies [L]) [G] Gone Return 410 (implies [L]) URL / Query String [QSA] Query string append Append original query string to substitution [QSA,L] [P,QSA] [QSD] Query string discard Drop original query string entirely [R=301,QSD,L] [B] Escape backrefs Re-encode special chars in backreferences [B,PT] [NE] No escape Don't escape special chars in output (pass #, ? through) [R=301,NE,L] Metadata and Handlers [E] Set env var Set an environment variable [E=VAR:val,L] [T] MIME type Force content type [H] Handler Force a content handler [PT] Pass through Pass result to next handler (needed with Alias/ScriptAlias) [PT,L] Cookie [CO] Set cookie Set an HTTP cookie on the response [CO=name:val:.domain,R=302,L] B (escape backreferences) ¶ The [B] flag instructs RewriteRule to escape non-alphanumeric characters before applying the transformation. mod_rewrite has to unescape URLs before mapping them, so backreferences are unescaped at the time they are applied.

Using the B flag, non-alphanumeric characters in backreferences will be escaped. For example, consider the rule: For similar escaping of server variables, see the "escape" mapping-function RewriteRule "^search/(.*)$" "/search.php?term=$1" Given a search term of 'x & y/z', a browser will encode it as 'x%20%26%20y%2Fz', making the request 'search/x%20%26%20y%2Fz'. Without the B flag, this rewrite rule will map to 'search.php?term=x & y/z', which isn't a valid URL, and so would be encoded as search.php?term=x%20&y%2Fz= , which is not what was intended.

With the B flag set on this same rule, the parameters are re-encoded before being passed on to the output URL, resulting in a correct mapping to /search.php?term=x%20%26%20y%2Fz . RewriteRule "^search/(.*)$" "/search.php?term=$1" [B,PT] Note that you may also need to set AllowEncodedSlashes to On to get this particular example to work, as httpd does not allow encoded slashes in URLs, and returns a 404 if it sees one. This escaping is particularly necessary in a proxy situation, when the backend may break if presented with an unescaped URL.

An alternative to this flag is using a RewriteCond to capture against %{THE_REQUEST} which will capture strings in the encoded form. In 2.4.26 and later, you can limit the escaping to specific characters in backreferences by listing them: [B=#?;] . Note: The space character can be used in the list of characters to escape, but you must quote the entire third argument of RewriteRule and the space must not be the last character in the list. # Escape spaces and question marks.

The quotes around the final argument # are required when a space is included. RewriteRule "^search/(.*)$" "/search.php?term=$1" "[B= ?]" To limit the characters escaped this way, see #flag_bne and #flag_bctls See URL Encoding and Decoding for a full explanation of how Apache decodes URIs before pattern matching. BNP|backrefnoplus (don't escape space to +) ¶ The [BNP] flag instructs RewriteRule to escape the space character in a backreference to %20 rather than '+'.

Useful when the backreference will be used in the path component rather than the query string. # Escape spaces to %20 in the path instead of + as used in form submission via # the query string RewriteRule "^search/(.*)$" "/search.php/$1" "[B,BNP]" This flag is available in version 2.4.26 and later. See URL Encoding and Decoding for background on how encoding is handled in the rewrite pipeline. BCTLS ¶ The [BCTLS] flag is similar to the [B] flag, but only escapes control characters and the space character.

This is the same set of characters rejected when they are copied into the query string unencoded. # Escape control characters and spaces RewriteRule "^search/(.*)$" "/search.php/$1" "[BCTLS]" This flag is available in version 2.4.57 and later. BNE ¶ The list of characters in [BNE=...] are treated as exclusions to the characters of the [B] or [BCTLS] flags. The listed characters will not be escaped. # Escape the default characters, but leave / RewriteRule "^search/(.*)$" "/search.php?term=$1" "[B,BNE=/]" This flag is available in version 2.4.57 and later.

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, are skipped. # Rewrite legacy product URLs to the new catalog app, # and add a tracking parameter — but only for the rewritten ones.

RewriteRule "^/products/([0-9]+)$" "/catalog/item/$1" [C] RewriteRule "^/catalog/(.*)$" "/catalog/$1?via=legacy" [QSA] Without the [C] flag, the second rule would also match requests that arrive at /catalog/ directly. The chain ensures the second rule is only applied when the first rule matched. 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 five optional fields.

The full syntax for the flag, including all attributes, is as follows: [CO=NAME:VALUE:DOMAIN:lifetime:path:secure:httponly:samesite] Security Warning Exercise care when constructing the argument from backreferences or other variable expansion. If any part of the argument is derived from user input, a malicious request may include delimeters or other unexpected values. If a literal ':' character is needed in any of the cookie fields, an alternate syntax is available.

To opt

The rest of this page is on the live site.