Wei Yu
Wei Yu

Reputation: 119

Remove a specific query parameter from URL

I have a URL such as:

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

I want 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?abc=one&def=two

This specific parameter can be anywhere in the URL with respect to other parameters.

The question looks very simple, but I tried many times but failed in the end.

Upvotes: 2

Views: 738

Answers (1)

Richard Smith
Richard Smith

Reputation: 49672

The entire query string is present in the $args variable or at the end of the $request_uri variable. You will need to construct a regular expression to capture everything before and after the part you wish to delete.

For example:

if ($request_uri ~ ^(/something\?.*)\bunwanted=[^&]*&?(.*)$ ) 
{
    return 301 $1$2; 
}

See this document for more, and this caution on the use of if.

Upvotes: 2

Related Questions