5

Let's say I have a URL such as:

http://www.example.com/something.html?abc=one&def=two&unwanted=three

I would like to remove the URL parameter unwanted and keep the rest of the URL in tact and it should look like:

http://www.example.com/something.html?abc=one&def=two

This specific parameter can be anywhere in the URL with respect to other parameters. The redirect should work regardless.

Can this be achieved?

3
  • possible duplicate of Remove parameters within nginx rewrite Commented Oct 15, 2014 at 14:16
  • 1
    No, in that question, the OP asks to remove ALL query parameters from URL, as opposed to a specific query parameter. Commented Oct 15, 2014 at 14:20
  • Seems like this can only be done in scripts, such as lua scripts (which will require to install a lua module into the nginx). Example like: ruby-forum.com/topic/4417604 Commented Oct 15, 2014 at 15:45

3 Answers 3

9

You can achieve that this way

if ($request_uri ~ "([^\?]*)\?(.*)unwanted=([^&]*)&?(.*)") {
    set $original_path $1;
    set $args1 $2;
    set $unwanted $3;
    set $args2 $4;
    set $args "";

    rewrite ^ "${original_path}?${args1}${args2}" permanent;
}
Sign up to request clarification or add additional context in comments.

1 Comment

great answer! I really don't want to write lua script.
5

I wanted to do something similar to this and here's the result ammended back to the context of the original question (regex is based on Milos Jovanovic's Answer)

if ($request_uri ~ "([^\?]*)\?(.*)unwanted=([^&]*)&?(.*)") {
  set $args $2$4;

  rewrite "^" $scheme://$host$uri permanent;
}

this way we only set one variable and if the updated $args is empty we dont have an unwanted ? at the end of the url as the server handles that itself

Comments

0

An other way is to redirect the client.

Of course it would work only for GET or HEAD, not if request has a body (ie: POST PUT PATCH)

BTW: in the specific case of fbclid the second line in map isn't required as the param is always appended to the url


map $request_uri $redirect_fbclid {
  "~^(.*)(?:[?&]fbclid=[^&]*)$"         $1;
  "~^(.*)(?:([?&])fbclid=[^&]*)&(.*)$"  $1$2$3;
}

if ( $redirect_fbclid ) {
  return 301 $redirect_fbclid;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.