Menu Close

Most commonly used flags in RewriteRule | mod_rewrite flags

4.1
(14)

Last Updated on August 17, 2021 by Amit

Flags are optional in mod_rewrite. They are used to change behaviour of RewriteRule directive. A RewriteRule can have at least one or multiple flags separated by a comma , character.
In RewriteRule flags are used inside sequere brackets [] .
The basic use of flags in RewriteRule is to instruct the engine how the rule should handle an http request. For example a rule with NC flag tells the engine to ignore URI character case so both Uppercase and Lowercase URIs are accepted by RewriteRule instead of just accepting lowercase URI which is the default behaviour.

This article covers all the most commonly used flags in RewriteRule with code examples and explainations on how they work.

Table of contents (Most used flags in RewriteRule)

[L] flag in RewriteRule

L stands for Last . We use L flag in RewriteRule to tell server that the rule is last. L flag gives instruction to server not to execute other rules if the specific one matches.

Forexample : If you have the following two rules in .htaccess file :

RewriteRule ^this /thispath
RewriteRule ^this /secondpath

As you can see the flag is ommited in rules above , so a request for /this will serve you the secondpath instead of the first rule’s destination path.
L flag is especially desigined to solve this type of rule’s confliction.

[NC] flag in RewriteRule

NC stands for NoCase . As the name states, this flag is used to match an URI in a case-insenstive manner meaning that your URI can contain both uppercase and lowercase URI characters.
The following is an example of NC flag in a RewriteRule :

RewriteRule ^hello$ /file.php [NC]

As you can we used NC in the rule above. With NC the Rule above will accept both URIs /hello and /HeLLo as the Case is ignored by NC flag.

[R] flag in RewriteRule

R stands for
Redirect . We use R flag in RewriteRule to redirect an URI from one location to the other. By default R redirects an URI using 302 redirect status which is temporary Redirect. You can manully append Redirect status code to R flag to change the redirect status ie: R=301 , R=403 etc.

See the rule example bellow to better understand how it works:

RewriteRule ^thisPath$ /thatPath [R=301]

The rewriteRule above will redirect /thisPath to /thatPath using the HTTP redirect status 301 changing the browser URL from old to new.

[QSA] flag in RewriteRule

QSA stands for
QueryStringAppend .
We use QSA in RewriteRule directive to combine new and old querystrings for example /this?q=bar to /that?query1=foo . QSA flag appends the old querystring to new target path like so /that?query=foo&q=bar.
You must use QSA in a RewriteRule when the target path of your rewriteRule already contains a querystring and you want to pass additional query perameters via your new URL to the old one.
The Rule example bellow will help you better understand the importance of QSA :

RewriteRule ^this.php$ /that.php?page=2 [QSA]

The rule above does two things, it rewrites /this.php to /that.php?page=2 and it will also pass additional query strings from your new URL path to the target path

forexample: /this.php?lang=en will also successfully map to /that.php?page=2&lang=en.

But if you remove the QSA then you wont get the same behaviour. Without QSA flag the additional query perameters you pass will get descarded.

[NE] flag in RewriteRule

NE | NoEscape flag tells apache not to convert special URI chracters like # , ? .. to their hex code. On apache server by default theses characters get changed ie # becomes %23 which can cause a not found url error on your server. See the example bellow we are using to redirect to a fregment path.

RewriteRule ^this$ /that#a [R,NE]

With NE flag the rule will redirect a request for /this$ to /that#a . If the NE is ommited then the URL you will land on would look something like /that%23a which is an invalid path.

[P] proxy flag in RewriteRule

P stands for Proxy . This flag is use in RewriteRule to proxy requests between two domains or two two different http hosts.
With P flag it is possible to rewrite your domain name to another domain without changing the URL in browser address bar.
To use P flag , first you must enable Proxy module on your Apache web server otherwise you will get a 500 “internal server error” .
The following is an example of P flag in a RewriteRule .

RewriteEngine on
RewriteRule (.*) https://example.com [P]

This will internally map all requests from your domain to example.com domain.

For example , a request for https://yoursite.com/* will get served from https://example.com without the URL change.

How useful was this post?

Click on a star to rate it!

Average rating 4.1 / 5. Vote count: 14

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code