Nginx

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: looklinux.com to www.looklinux.com.

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 = 'looklinux.com' ) {
         rewrite  ^/(.*)$  http://www.looklinux.com/$1  permanent;
      }

Save and close the file.

Reload Nginx

# service nginx relaod

Testing

Open your browser and type the below url:

http://looklinux.com/

It should be redirected to

http://www.looklinux.com/

Thanks:)

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 www.looklinux.com. To become an author at LookLinux 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