Last Updated on November 5, 2021 by Amit
In this article I will show you how mod-rewrite QSA flag works and why it’s important to use with RewriteRules in an htaccess file.
What is QSA in htaccess
Are you wondering what this three letter flag QSA is and why it’s used ?
Let me first tell you the meaning of this flag.
QSA stands for QueryStringAppend .
As the name applies, this flag appends the query strings.
QSA flag is used with RewriteRules to combine new and old queryStrings together.
On Apache mod_rewrite, queryStrings are discarded by default if your RewriteRule already contains a query String in the destination path. So we use this flag to fix the problem.
I know you won’t understand it better with just the definition , so here are some code examples that explain the job of QSA in way that will make the process easier for you to understand how it actually works.
We have the following RewriteRule to rewrite /foobar/ to /demo.php .
RewriteEngine on RewriteRule ^foobar/$ /demo.php [L]
With the rule above you can just type /foobar/ to view the contents of /demo.php file.
This will also rewrite your new URL with queryString , for example /foobar/?q=bar . This will map the new URL to /demo.php?q=bar .
Perfact? Do you need the QSA here?
No : because the target path in our rule is not using any queryStrings so the new queryStrings are automatically appended.
Now see the following Rule :
RewriteEngine on RewriteRule ^foobar/$ /file.php?q=foobar [L]
This rule maps /foobar/ to /file.php?q=foobar.
As you can see the target path contains a query string ?q=foobar .
Now what if you type the new URL with a new queryString /foobar/?n=123 .
The rule will not map your new queryString ?n=123 to the target path as the target already has an old queryString.
The Solution to this is to use QSA .
With QSA, your rule will map your new URL path /foobar/?n=123 to /file.php?q=foobar&n=123 .
RewriteEngine on RewriteRule ^foobar/$ /file.php?q=foobar [L,QSA]
Related post : Mod_rewrite QSD flag .