im_gm
im_gm

Reputation: 3

htaccess: try to cut parameter from url, keep the other optional parameters

I'm trying to cut a parameter from URLs but keep the others and I never know the value.

www.example.com/?useless=somevalue&foo=somevalue&bar=somevalue
to =>
www.example.com/?foo=somevalue&bar=somevalue

www.example.com/?useless=somevalue
to =>
www.example.com/

www.example.com/?foo=somevalue&bar=somevalue&useless=somevalue
to =>
www.example.com/?foo=somevalue&bar=somevalue

Upvotes: 0

Views: 133

Answers (2)

im_gm
im_gm

Reputation: 3

What I Have right now:

If Parameter is in the middle

RewriteCond %{QUERY_STRING} ^(.+)&useless=[0-9a-z_-]+&(.+)$ [NC]
RewriteRule (.*) $1?%1&%2 [R=301,L]

If Parameter is the last one

RewriteCond %{QUERY_STRING} ^(.+)&useless=[0-9a-z_-](.+)$ [NC]
RewriteRule (.*) $1?%1 [R=301,L]

If it's the first but has some following

RewriteCond %{QUERY_STRING} ^(.+)?useless=[0-9a-z_-]+&(.+)$ [NC]
RewriteRule (.*) $1?%1%2 [R=301,L]

But can't figur out the condition and rule for if it's the only Parameter

Upvotes: 0

anubhava
anubhava

Reputation: 785246

You can use this rule to remove a named query parameter from any position:

RewriteEngine On

RewriteCond %{THE_REQUEST} \?(.*&)?useless=[^&]*&?(\S*)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=301,NE,L]

Upvotes: 1

Related Questions