Gonzalo
Gonzalo

Reputation: 369

Nginx location redirect but saving the arguments passed

I am trying to redirect users to a new URL while preserving the URL query parameters. I'm trying this, which doesn't pass the URL params:

location = /api/redirects {
  return 301 /api2/redirects;
}
https://example.com/api/redirects?param=1&anotherParam=10

  => https://example.com/api2/redirects

I also tried:

location = /api/redirects {
  return 301 /api2/redirects$is_args$args;
}

Upvotes: 1

Views: 3130

Answers (1)

anthumchris
anthumchris

Reputation: 9090

You can use rewrite explicitly outside of a location block. RegEx parentheses can capture URI values after /api/ as $1. The query string is always passed in a rewrite directive.

# 302 Moved Temporarily
rewrite ^/api/(redirects.*) /api2/$1 redirect;
# 301 Moved Permanently
rewrite ^/api/(redirects.*) /api2/$1 permanent;

Upvotes: 1

Related Questions