nginx evil if alternative?

JCats asked:

Looking to see if there is a better way to redirect a query string other than using if in this situation.

Example URL: to

Current using if:

location / {
  if ($args ~ p=123) {
  rewrite ^ permanent;

My answer:

You should check the actual argument value, so that you don’t accidentally match things like op=1234 when you wanted to match p=123.

For performance reasons, you should also use return instead of rewrite unless you actually need regex matching.

For example:

if ($arg_p = 123) {
    return 301;

(And of course, return is perfectly fine to use with if.)

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.