2017-01-31 08:41:58 +00:00
|
|
|
---
|
|
|
|
title: Nginx Redirects With Query String Arguments
|
2020-03-08 14:32:13 +00:00
|
|
|
date: 2017-01-31
|
2020-03-08 17:52:59 +00:00
|
|
|
excerpt:
|
|
|
|
How to redirect from an old domain to a new one, and also to redirect from the
|
|
|
|
root example.com domain to the canonical www subdomain.
|
2017-01-31 08:41:58 +00:00
|
|
|
tags: [nginx]
|
|
|
|
---
|
2020-03-08 17:52:59 +00:00
|
|
|
|
|
|
|
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 `example.com` domain to
|
|
|
|
the canonical `www` subdomain.
|
2017-03-16 08:09:52 +00:00
|
|
|
|
|
|
|
```language-nginx
|
2017-01-31 08:41:58 +00:00
|
|
|
server {
|
|
|
|
listen 80;
|
|
|
|
|
|
|
|
server_name example.com;
|
|
|
|
server_name my-old-domain.com;
|
|
|
|
server_name www.my-old-domain.com;
|
|
|
|
|
|
|
|
return 301 https://www.example.com$uri;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
It also redirects the URI value, e.g. from `http://example.com/test` to
|
|
|
|
`http://example.com/test`, but I noticed recently though that any the query
|
|
|
|
string would be lost - e.g. `http://example.com/?test` would redirect to
|
|
|
|
`http://www.example.com` 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.
|
2017-01-31 08:41:58 +00:00
|
|
|
|
|
|
|
This was fixed by making a small change to my `return` statement.
|
|
|
|
|
|
|
|
Before:
|
|
|
|
|
2017-03-16 08:09:52 +00:00
|
|
|
```language-nginx
|
|
|
|
return 301 https://www.example.com$uri;
|
|
|
|
```
|
2017-01-31 08:41:58 +00:00
|
|
|
|
|
|
|
After:
|
|
|
|
|
2017-03-16 08:09:52 +00:00
|
|
|
```language-nginx
|
|
|
|
return 301 https://www.example.com$uri$is_args$args;
|
|
|
|
```
|
2017-01-31 08:41:58 +00:00
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
`$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).
|
2017-01-31 08:41:58 +00:00
|
|
|
|
|
|
|
Here is an demo of it working on this website:
|
|
|
|
|
2018-09-04 18:58:54 +00:00
|
|
|
![](/images/blog/nginx-redirect-with-args.gif)
|
2017-01-31 08:41:58 +00:00
|
|
|
|
|
|
|
## Resources
|
|
|
|
|
|
|
|
- [Query string](https://en.wikipedia.org/wiki/Query_string)
|
|
|
|
- [Nginx ngx_http_core_module](http://nginx.org/en/docs/http/ngx_http_core_module.html)
|