Nginx

Nginx : Redirect Old Domain Name To New Domain Name

redirect-old-domain-to-new-domain
mm
Written by Santosh Prasad

I have changed the domain name from old domain name such as bala.bala.com to www.bla.com which is shorter and simple to recall. How would I divert old domain to new domain with “HTTP/1.1 301 Moved Permanently” status code under Nginx web server running on Unix like working frameworks?

Nginx by default comes with module called HttpRewriteModule. This module is used to change URI using regular expressions.

Redirect Old Domain Name To New Domain Name

Edit the nginx configuration file /etc/nginx/nginx.conf.

# vim /etc/nginx/nginx.canf 

Now add the below line in server {….} block location after server_name directive:

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

With the above redirect your all file and folder will redirect to http://www.bla.com permanently.

For example;

http://bala.bala.com/ to http://www.bla.com/
http://bala.bala.com/index.html to http://www.bla.com/
http://bala.bala.com/folder to http://www.bla.com/folder

Using above url will redirect with 301 http status code.

Nginx Sample Code

### redirect bala.bala.com$URI to www.bla.com$URI with 301 ###
  server {
    listen      192.168.0.5:80;
    server_name bala.bala.com;
    root        /var/www/html/;
    index       index.html;
    rewrite     ^ $scheme://www.bla.com$request_uri permanent;
    # .... 
 }

Save and closer file.

Thanks:)

Thank you! for visiting Look Linux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at Look Linux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment