Reputation: 125
I'm trying to remove some parameters using nginx return 301.
For example I want URLs redirected like this:
https://www.example.com/one?wanted=1&unwanted1=1
-> https://www.example.com/one?wanted=1
https://www.example.com/one?unwanted1=1
-> https://www.example.com/one
https://www.example.com/one?unwanted1=1&unwanted2=2&unwanted3=3
-> https://www.example.com/one
...and so on
I did some experiments with this code (note it uses rewrite instead of return):
if ($request_uri ~ "([^\?]*)\?(.*)unwanted=([^&]*)&?(.*)") {
set $args $2$4;
rewrite "^" $scheme://$host$uri permanent;
}
This works well when only one of the unwanted parameters are present.
I tried to repeat it for each parameter but now it doesn't work in an optimum way as it does multiple redirects and does not clean up the "&" when it is no longer needed (only one arg left).
...moreover - I would prefer to use return 301 instead of rewrite as I understand it would be the preferred way in these kind of situations. Tried to adapt the last line to return 301 but it didn't work out for me.
Any ideas how I can correctly accomplish this?
Upvotes: 2
Views: 2468
Reputation: 49672
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.
Upvotes: 1