Search code examples
.htaccessparametersquery-string

How to remove specific QUERY_STRING parameter in .htaccess


I need a proper solution to delete the URL parameter.

Example:

Input: https://www.hostever.com/blackfriday/?fbclid=IwAR3s1aVKUQELAb0EGW9_mh4qyR-i9ZqfNjFFB6xv_MoNRal2cH--lKofqHM

Output: https://www.hostever.com/blackfriday/

So it will delete the fbclid=

Input: https://www.hostever.com/?s=blogger
Output: https://www.hostever.com/?s=blogger
No need to change other parameters except **fbclid=**

I( need to delete only fbclid= this one from the URL.

Now I am using this, but the problem is with many parameters.

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{QUERY_STRING} !=""
 RewriteCond %{QUERY_STRING} !^p=.*
 RewriteCond %{QUERY_STRING} !^action=.*
 RewriteCond %{QUERY_STRING} !^s=.*
 RewriteCond %{REQUEST_URI} !^/wp-admin.*
 RewriteRule ^(.*)$ /$1? [R=301,L]
</IfModule>

Please help me, How can I delete a specific parameter from the URL with .htaccess?


Solution

  • I think Stephen had the right idea. But for what it's worth, I only needed one rule to remove three types of trackers:

    <IfModule mod_rewrite.c>
    RewriteEngine On
      RewriteCond %{QUERY_STRING} ^(.*)(fbclid|gclid|utm_)[^\&]*\&(.*)$ 
      RewriteRule ^(.*)$ /$1?%1%3 [R=301,L]
    </IfModule>
    

    I needed this for a Wordpress blog, because when Wordpress tried to process parameters, the bogus ones caused a 404.  (Even when the URI was to a non-Wordpress static page on the site!)

    The first set of parentheses matches everything before either of the three bad guys.  The third matches everything after the &.  The condition then puts the first and third together.  Stephen's comment about it depending on the & being present is correct—it does not remove the fbclid! (unless followed by an ampersand).  And yet, for reasons I don't understand, adding this rule made Wordpress stop saying 404 on links from Facebook.

    However, my rule will not work if fbclid is in the URI outside of the parameters.  For example, any page on a domain named fbclid-sucks.com :-)