Last Updated on May 21, 2021 by Amit
Five commonly used Flags in Apache mod rewrite
Flags play a important role in RewriteRule directive by changing its behavior. Although flags are optional in RewriteRule but we can’t rewrite an URL the way we want to do without using Flags. In this post I am going to share 5 rules that you often see in RewriteRules .
The following 5 rules are supported in all apache versions.
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 : You could have the following two rules in your 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 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 Upper case and lower case URI characters.
The following is an example of NC flag in a RewriteRule directive :
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 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 appnd Redirect status code to R flag to change the redirect status ie: R=301 , R=403 etc.
See the rule example bellow:
RewriteRule ^this$ /that [R=301]
The rewriteRule above will redirect /this to /that using the HTTP redirect status 302 and changing the browser URL from old to new.
QSA means 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 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 will rewrite /this.php to /that.php?page=2 . With the rewriteRule above you can also pass an additional querystring ?query. forexample: /this.php?lang=en will successfully map to /that.php?page=2&lang=en.
But if you remove the QSA then you wont get the behaviour. Without QSA flag the additional query perameters you pass will get descarded.
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.