user1754738
user1754738

Reputation: 347

Apache Remove Specific Query Strings

I want to remove specific query strings from any URL leaving other query strings in tact, as well as account for 1st versus subsequent strings (? versus &). I only wish to remove the following URL parameters:

campaign external

It's important to account for the fact that these URL parameters may exist anywhere in the URL so the remaining parameters may need to be flipped to "?" from "&".

Here are a few examples:

Original: www.domain.com/page.html?campaign=123
Modified: www.domain.com/page.html

Orig: www.domain.com/page2.html?campaign=123&flow=shop
Mod: www.domain.com/page2.html?flow=shop

Orig: www.domain.com/page6.html?flow=account&campaign=123&entry=1&external=google
Mod: www.domain.com/page6.html?flow=shop&entry=1

Upvotes: 1

Views: 209

Answers (1)

anubhava
anubhava

Reputation: 785761

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^(?:(.+?)&)?(?:external|campaign)=[^&]*$ [NC,OR]
RewriteCond %{QUERY_STRING} ^(?:external|campaign)=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1 [L,NE,NC,R=302]

RewriteCond %{QUERY_STRING} ^(?:(.+?)&)?(?:external|campaign)=[^&]*(&.*)?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [L,NE,NC,R=302]

Upvotes: 1

Related Questions