Skip to main content

Nginx Redirects With Query String Arguments

Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

This is an example of how my Nginx configuration looked to redirect from an old domain to a new one, and also to redirect from the root domain to the canonical www subdomain.

server {
  listen 80;


  return 301$uri;

It also redirects the URI value, e.g. from to, but I noticed recently though that any the query string would be lost - e.g. would redirect to and the ?test would be dropped. The application that I built references images based on the query string, so I wanted these to be included within the redirect.

This was fixed by making a small change to my return statement.


return 301$uri;


return 301$uri$is_args$args;

$is_args is an empty string if there are no arguments, or a ? to signify the start of the query string. $args then adds the arguments ($query_string could also be used with the same result).

Here is an demo of it working on this website:


Comments or questions? I'm @opdavies on Twitter.