1

The URL(s) like below (there are 2 websites, one with www and other without it)

https://example1.com/any-path-here/?_gl=1*um0ntk*_ga*YW1wLUVyYS1QbHgwcFp1SEJBYVlGSlNYdWNQUWIxNGVxY2VhakRDNDUtYmJUZWZPV1M2WEdWN2s2dXVRNzNZWGNTdkk

https://www.example2.com/hello-how-are-you-today/?_gl=1*1lcwex2*_ga*YW1wLXBtWXdrdWNEcTRjTVcwWjMzTzJwNEdJeU5vTmo5UVF0RmFhTEdpbkdMdjgyLXRNQXJaM21oRGpUX2ppSnB3dGs

should redirect to

https://example1.com/any-path-here/
https://www.example2.com/hello-how-are-you-today/

Only the _gl= seems to be constant.

I have tried:

<IfModule mod_rewrite.c>
RewriteEngine On  
RewriteCond %{QUERY_STRING} _gl=    
RewriteRule ^ %{REQUEST_URI}? [L,R]
</IfModule>

And

<IfModule mod_rewrite.c>
RewriteEngine On  
RewriteCond %{QUERY_STRING} ^_gl=*    
RewriteRule ^ %{REQUEST_URI}? [L,R]
</IfModule>

And other rules. But nothing seems to work.

I hope the solution won't mess up the naked domain redirect and https redirect.

  • I have not included the rules for the naked domain redirect and https redirect in the above question. But those rules are also present in the .htaccess file. – M.K.Dan Oct 13 at 13:50
0

Whilst neither of the rules you've posted are strictly correct, they should both perform the required redirect. If you have other directives in your .htaccess file then the most likely cause is that they are in the wrong order and you have a conflict.

These redirects will need to go near the top of the .htaccess file, before any existing mod_rewrite directives. (Although you may want to combine it with your HTTPS / naked domain redirects?)

You don't need the <IfModule> wrapper and you don't need the RewriteEngine On directive, if this already exists in your .htaccess file.

To remove the query string when a request contains a query string that starts _gl= then the CondPattern should read ^_gl=. A trailing * repeats the preceding = and _gl= matches that string anywhere in the query string, not just at the start.

For example:

RewriteCond %{QUERY_STRING} ^_gl=    
RewriteRule ^ %{REQUEST_URI}? [R,L]

If you are on Apache 2.4 then you can use the QSD flag (Query String Discard) flag instead of appending a ? to the substitution string. ie. RewriteRule ^ %{REQUEST_URI} [QSD,R,L]

  • 1
    Thank you so much. It works. I just moved it to the top of other RewriteRules. – M.K.Dan Oct 13 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.