Nginx: 301 Redirect To A Domain Name

by on November 19, 2013 · 4 comments· LAST UPDATED March 15, 2014

in , ,

I am a new Nginx web server user. How do I redirect to a different domain using nginx (say example.org to example.com) permanently?

You need to use HttpRewriteModule module to change URI using regular expressions (PCRE), and to redirect and select configuration depending on variables.

Tutorial details
DifficultyEasy (rss)
Root privilegesYes
RequirementsNginx
Estimated completion time5m

rewrite directive

This directive changes URI in accordance with the regular expression and the replacement string. Directives are carried out in order of appearance in the configuration file. You can use this directive in the following context:

  • server
  • if
  • location

Nginx 301 rewrite syntax

The syntax is:

rewrite regex replacement [ flag ]

Flags can be any of the following:

  1. last - completes processing of current rewrite directives and restarts the process (including rewriting) with a search for a match on the URI from all available locations.
  2. break - completes processing of current rewrite directives and non-rewrite processing continues within the current location block only.
  3. redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://
  4. permanent - returns permanent redirect with code 301

Example: Redirect to a different domain with nginx

In this example, redirect all traffic as follows:

From:To:
example.orgexample.com
example.org/foo.htmlexample.com/foo.html
example.org/show.php?arg=valueexample.com/show.php?arg=value
example.org/dir1/dir2/bar.cgiexample.com//dir1/dir2/bar.cgi

Edit nginx.conf or your virtual host config and update server context as follows:

server {
  server_name .example.org;
  rewrite ^ http://example.com$request_uri? permanent;
  # rest of config, if any goes below ... #
}

Finally, restart/reload your nginx server, type:
# nginx -t && nginx -s reload

How do I test and verify HTTP 301 redirect?

You need to use curl command:
$ curl -I http://example.org/
$ curl -I http://example.org/foo/bar.html

Sample outputs:

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 18 Nov 2013 12:29:08 GMT
Connection: keep-alive
Keep-Alive: timeout=60
Location: http://example.com/foo/bar.html
Vary: Accept-Encoding
X-Galaxy: Andromeda-2
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 4 comments… read them below or add one }

1 Alexander Kuznetcov November 19, 2013 at 11:41 am

Normally, you should use separate server block with ‘return’ directive, instead of ‘rewrite’ directive inside of main server block, for redirects
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#return

server {
  server_name .example.org;
  return 301 $scheme://example.com$request_uri;
}
server {
  server_name example.com;
  # rest of config, if any goes below ... #
}

Reply

2 nixCraft November 19, 2013 at 6:10 pm

Good point. I appreciate your post.

Reply

3 Jean November 21, 2013 at 4:46 am
4 Bob November 30, 2013 at 11:13 am

Excellent was looking for this tutorial.

Reply

Leave a Comment

Tagged as: , , , , , ,

Previous Faq:

Next Faq: