Nginx : Force Redirect Non-WWW To WWW Domain

We know how to redirect old domain name to new domain name in Nginx, In this article I will explain how to permanently redirect non-www to www domain for example: to

For good SEO purposes you can easily redirect non-www to www domain using following syntax.

Force Redirect Non-WWW To WWW Domain

Edit the nginx.conf file to force non-www to www domain.

# vim /etc/nginx/nginx.conf

### redirect non-www to www with client code 301 ###
      if ($host = '' ) {
         rewrite  ^/(.*)$$1  permanent;

Save and close the file.

Reload Nginx

# service nginx relaod


Open your browser and type the below url:

It should be redirected to


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