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?