In this short tutorial I’ll show you how to make permanent redirect from a www URL to non-www and vice versa. I’ll assume that you have superuser privileges, sudo or root access and Nginx already configured, as well as DNS records. More specifically, you need to have an A records for www.yourdomain.com and yourdomain.com .
Redirect non-www to www
To redirect users from a plain, non-www domain to a www domain, you need to add this snippet in your Nginx domain configuration file:
1 2 3 4 |
server { server_name yourdomain.com return 301 http://www.yourdomain.com$request_uri; } |
Save your configuration and exit. Before restarting Nginx make sure to test your configuration:
1 |
root@secure:~# nginx -t |
Continue reading How To Redirect www To non-www And Vice Versa with Nginx