nginx return 301 remove some parameters nginx return 301 remove some parameters nginx nginx

nginx return 301 remove some parameters


To remove multiple unwanted arguments in an indeterminate order, you will need to use some form of recursion. Your question contains an example that uses an external redirection loop, but suffers from undesired edge cases.

The edge cases of leaving trailing ? and & can be cleared up using multiple if...return blocks.

For example:

if ($args ~ ^(?:unwanted1|unwanted2)=[^&]*$ ) {    return 301 $uri;}if ($args ~ ^(?:unwanted1|unwanted2)=[^&]*(?:&(.*))?$ ) {    return 301 $uri?$1;}if ($args ~ ^(.*)&(?:unwanted1|unwanted2)=[^&]*(&.*)?$ ) {    return 301 $uri?$1$2;}

The above works by treating the two cases when a trailing ? or trailing & occurs, separately with a specially crafted return statement.

The above works by using an external redirection loop.


nginx can also perform internal redirection, and one method to achieve this is to use a rewrite...last from within a location block.

The restriction in this case would be to find the location block which will process the set of URIs that are affected by the unwanted arguments. In the example below, I use the location / block, but your requirements will be affected by the other location blocks within your configuration and the set of URIs affected.

Although you have clearly had success with assigning to the internal $args variable, my answer avoids that technique.

The following example reimplements the previous example using rewrite...last and adds a forth if block to perform the actual return 301.

location / {    if ($args ~ ^(?:unwanted1|unwanted2)=[^&]*$ ) {        rewrite ^ $uri? last;    }    if ($args ~ ^(?:unwanted1|unwanted2)=[^&]*(?:&(.*))?$ ) {        rewrite ^ $uri?$1? last;    }    if ($args ~ ^(.*)&(?:unwanted1|unwanted2)=[^&]*(&.*)?$ ) {        rewrite ^ $uri?$1$2? last;    }    if ($request_uri ~ \?(.*&)?(unwanted1|unwanted2)= ) {        return 301 $uri$is_args$args;    }    try_files $uri $uri/ =404;}

Note that the example uses only permitted statements within the if blocks as indicated in this document.