Header image
How to do URL Redirection with Nginx

How to do URL Redirection with Nginx

August 2, 2018Tips and Tricks

URL Redirection is a most known terms in Web Design and development field. Usually a redirect is a way to send both users and search engines to a different URL from the one they originally requested. There are so many redirection you may see, but three most commonly used redirects are 301, 302, and Meta Refresh.

NGINX (Engine x) is a very powerful web server and uses a non-threaded, event-driven architecture that enables it to outperform Apache if configured correctly. NGINX is very popular among developer and companies, because its a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.

URL Redirection with Nginx

Recently I worked in website at Melo Pixels office., where I did more than 1000 URL Redirection. Today I am going to share my experience, how I did that much URL redirection using nginx.

Normally a rewrite rule expects 3 arguments, which are:

  1. A regex capture string
  2. A regex replacement string
  3. A rewrite type value (permanent, rewrite)

For example: rewrite /old-url https://melopixels.com/new-url permanent;

In above example /old-url will be redirected to https://melopixels.com/new-url

During adding new URL redirection rule, I was facing some issue with query string. I wanted to redirect with exact match for query string, which was not possible using redirect rule as it is doesn’t match query string.

For example, I want to redirect on exact match URL with query string from – https://melopixels.com/old-url?a=1&b=2&c=3
to https://melopixels.com/new-url?x=1&y=2&z=3

But when I use following configuration: rewrite /old-url?a=1&b=2&c=3 https://melopixels.com/new-url?x=1&y=2&z=3 permanent;

It was ignoring the query string match for the capture string and adding whatever query string is passed to the capture string as the query string for replacement string, it was redirecting to: https://melopixels.com/new-url?x=1&y=2&z=3&whatever-query-string-is-passed-at-capture-string

To do my things done I have modified the config as following:

location = /old-url {
 if ($args ~* "a=1&b=2&c=3") {
  set $args "x=1&y=2&z=3";
  rewrite ^.*$ https://melopixels.com/new-url permanent;

And it works for me as I wanted!

While applied this solution I faced another important issue that if the old-url and new-url are same while redirecting with location block it will fall into dead-loop so I have to be careful about it.

For example suppose I need to redirect on exact match with query string for

https://melopixels.com/common-url?a=1&b=2&c=3 to


Now if I try

location = /common-url {
 if ($args ~* "a=1&b=2&c=3") {
  set $args "x=1&y=2&z=3";
  rewrite ^.*$ https://melopixels.com/common-url permanent;

It will lead us to an infinite loop, in this case, we need to make sure that base URLs for old-url and new-url must not be same!

You may learn more:

Thanks and hope you can now do URL redirection using NGINX very easily.

Best of Luck.

Related post