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

Example URL: http://foo.com/?p=123 to http://foo.com/new

Current using if:

location / {
  if ($args ~ p=123) {
  rewrite ^ https://foo.com/new? permanent;

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 https://example.com/new;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.