Nginx Redirect Url with Query Strings

nginx 301 redirect with query string

Anything from the ? and after is the query string and is not part of the normalised URI used in location and rewrite directives. See this document for details.

If you want to keep the query string, either add it to the return:

location = /old/page/ {
return 301 /new/page$is_args$args;
}

Or with rewrite, the query string is automatically appended unless a ? is added:

rewrite ^/old/page/$ /new/page permanent;

See this document for location syntax, and this document for return/rewrite.

NGINX Redirect URL with Query Strings

The rewrite and location directives use a normalized URI which does not include the query string.

To test the query string, you will need to consult the $request_uri or $args variable using an if statement and/or map directive.

The advantage of using $request_uri is that it contains the original request and will help to avoid a redirection loop.

If you only have one redirection to perform, the map solution is probably overfill.

Try:

if ($request_uri ~ ^/index\.php\?tag=(.*)§ion=(.*)&type=(.*)$) {
return 301 /$1/$2/$3;
}
location / {
...
}

See this caution on the use of if.

NGINX redirect with query strings getting ignored

Anything from the ? and after is the query string and is not part of the normalized URI used in rewrite (and other) directives.

Typical solutions are using the map directive to map query string parameters to URIs, or not doing this type of redirection at the Nginx level, and using a plugin like Retour* to handle redirects.

  • Disclaimer: I'm the author of Retour.

Nginx redirect URL with specific query parameter on url

The characters such as ", <, and > are URL encoded in the original request and remain encoded in the $args variable.

Also, the ^& in your regular expression, anchors the & to the beginning of the query string, whereas there is actually another parameter in front of it.

The following example appears to work correctly:

location = /xyz {
if ($args ~* "&color=%22%3CK%3E%22") {
...
}
}

Alternatively, you can test the value of the color parameter directly using:

location = /xyz {
if ($arg_color = '%22%3CK%3E%22') {
...
}
}

Read query parameter from url to do a nginx redirect

While it is possible to do this in Nginx, it'll defeat some of the advantages that Nginx provides. If you can, it's better to do it within your application code or Lua.

If you choose to go forward with if statements, you'll want to match against individual $arg_* variables rather than $args. In particular, you'll want to use $arg_color, which will contains the color querystring value.

location = /pages {
if ($arg_color = "white") { return 301 /variant1$is_args$args; }
if ($arg_color = "red") { return 301 /variant2$is_args$args; }

# If we get to this point, there was no match. You have to keep
# this very simple and can only use directives documented as being
# compatible with `if`, or you'll get all sorts of crazy bugs and
# crashes.
return 404.
}

Nginx redirect to another page if url contain specific query string

The = operator does not understand wildcards such as *. You will need to use regular expression syntax.

For example:

if ($args ~* "^fl=&fl=") { ... }

See this document for details.

Nginx redirect URL with specific query parameter

Each query parameter is exposed as a variable prefixed with $arg_ in the configuration file. For example, device would become $arg_device. Using this you can make the comparison check within your location block, for example:

location / {
if ($arg_device = desktop) {
return 301 $uri;
}
}


Related Topics



Leave a reply



Submit