≡ Menu

Nginx Redirect (Rewrite) Old Domain To New Domain With HTTP 301

I‘ve changed the domain name from long-domain-name-example.com to example.com which is shorter and easy to remember. How do I redirect old domain to new domain with “HTTP/1.1 301 Moved Permanently” status code under nginx web server running on Unix like operating systems?

Nginx web server comes with module called HttpRewriteModule. Use this module to change URI using regular expressions (PCRE), and to redirect and select configuration depending on variables. All you have to do is capture old domain and redirect to new domain. Edit /usr/local/nginx/conf/nginx.conf or /etc/nginx/nginx.conf, run:
# vi /usr/local/nginx/conf/nginx.conf
Append the following line in server{ ... } block after declaring server_name:

rewrite ^ $scheme://www.example.com permanent;

In this case all requests to the uri such as long-domain-name-example.com, long-domain-name-example.com/file.html, long-domain-name-example.com/folder/file2.php, long-domain-name-example.com/cgi-bin/register.cgi?action=1 all are redirected to www.example.com with code 301. However, you may want to copy and pass the URL too, try:

 rewrite ^ $scheme://www.example.com$request_uri permanent;

The $request_uri variable is equal to the original request URI as received from the client including the args. In other word redirect url as follows with 301 status code:

http://long-domain-name-example.com to http://www.example.com
http://long-domain-name-example.com/file.html to http://www.example.com/file.html
http://long-domain-name-example.com/cgi-bin/register.cgi?action=1 to http://www.example.com/cgi-bin/register.cgi?action=1
http://long-domain-name-example.com/static/js/lib.js?v=2 to http://www.example.com/static/js/lib.js?v=2

Sample code block:

### redirect beta.cyberciti.biz$URI to www.cyberciti.biz$URI with 301 ###
  server {
    server_name beta.cyberciti.biz;
    root        /usr/local/nginx/html;
    index       index.html;
    rewrite     ^ $scheme://www.cyberciti.biz$request_uri permanent;
    # .... 

Save and close the file. Reload the nginx web-server, run:
# /usr/local/nginx/sbin/nginx -s reload

redirect vs permanent flag

The permanent flag redirect URL with http status code 301. It means that a page has permanently moved to a new location. This code is recommended by search engine giant Google:

301 redirects are particularly useful in the following circumstances:

  1. You’ve moved your site to a new domain, and you want to make the transition as seamless as possible.
  2. People access your site through several different URLs. If, for example, your home page can be reached in multiple ways – for instance, http://example.com/home, http://home.example.com, or http://www.example.com – it’s a good idea to pick one of those URLs as your preferred (canonical) destination, and use 301 redirects to send traffic from the other URLs to your preferred URL. You can also use Webmaster Tools to set your preferred domain.
  3. You’re merging two websites and want to make sure that links to outdated URLs are redirected to the correct pages.

The redirect flag returns temporary redirect with http status code 302. It means that a page has moved temporarily to a new location. This code is not recommended for domain change.

Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 0 comments… add one }
Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , ,