regex – Nginx check if parameters exists and if so only allow specific values

I cant seem to find a way to limit some queries. Right now i have this

if ($args ~* "(.*&)?page="){
   set $wrongP W;
}
if ($args !~* (.*&)?page=(2|12|24)$){
   set $wrongP "${wrongP}R";
}
if ($wrongP = WR){
   rewrite ^(.*)$ $uri? permanent;
}

This does exactly what i want, the problem is if url has more than one parameter fox ex. /somefile.html?page=2&arg=file it removes ALL parameters. Im guessing its because of the second if.

How do check for urls that have multiple variables and removes all of them only if page variable is not equal to 2, 14 or 24 ?

Read more here: Source link