How to set up a redirect with $args?

Please help to understand how the redirect, already a head broke and tried a thousand combinations.

What is: There are pagination pages format site.ru/?q=&p=331
What to do: Redirect to a page format site.ru/page/331

Or just give the 404 page
April 7th 20 at 15:26
2 answers
April 7th 20 at 15:28
location = / {
 if ($arg_p) {
 rewrite / /page/$arg_p? redirect;
}
}
April 7th 20 at 15:30
location / {
 if ($query_string ~ "(^|&)q\=($|&)"){
 if ($query_string ~ "(^|&)p\=([0-9]({1,})($|&)"){
 rewrite ^/$ /page/$2? redirect;
}
}
}

but then if itself is not the best idea
Can you make sense in 404.php to put a redirect on page
First you will not lose in speed because redirects will be triggered only if 404, respectively, will not load the server in the empty
The second redirects to page easier than nginx
In the template there is an extra parenthesis [0-9]({1,}

Well, in docks it is written that the nested "if" are not supported.
nginx.org/ru/docs/http/ngx_http_rewrite_module.html#if
Context: server, location
- cyril_Toy86 commented on April 7th 20 at 15:33
in docks it is written do not use if ;) this is an extreme method.
because it is not very productive about productivity. and generally do not fit into the philosophy of nginx ( look what the developer says about the variables and conditions of the configs, if in short and remove all the obscene something he is against) - Kobe.Huels38 commented on April 7th 20 at 15:36

Find more questions by tags Nginx