Nginx : Redirect Old Domain Name To New Domain Name

I have changed the domain name from old domain name such as to 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:// permanent;

With the above redirect your all file and folder will redirect to permanently.

For example; to to to

Using above url will redirect with 301 http status code.

Nginx Sample Code

### redirect$URI to$URI with 301 ###
  server {
    root        /var/www/html/;
    index       index.html;
    rewrite     ^ $scheme://$request_uri permanent;
    # .... 

Save and closer file.


Thank you! for visiting LookLinux.

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

About the author


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