March 17, 2021 0 Comments

Last Updated on April 14, 2021 by amitoverflow1

htaccess remove query string from URLs

QSD stands for “Query String Discard” as the name implies this flag is used to discard query string from URLs. This flag is supported on Apache versions 2.4+ . If you use this flag on earlier versions the you will get an “internal server error” as the flag is not defined on earlier versions of Apache. Read how to trim queryString on older versions .
We use this flag to remove query strings from URLs when we want a clean redirected URL.

Examples of QSD flag

Suppose you have a URL that looks something similar to

And want to convert this URL into a clean format that doesn’t contain query string parameters , something like the following URL :

To achieve this, we can use a RewriteRule with QSD flag that can redirect our old URL to the new one (from queryString to without queryString) .
We can use the following RewriteRule in our htaccess file to remove queryString : RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=foo&n=bar$
RewriteRule ^file.php$ /file.php [L,R,QSD]
Okay. So what the rule above does is it checks if the request is for “/file.php?q=foo&n=bar” if the condition is met then the rule redirects the URL to remove the queryString.

Leave a Reply

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