In this post I will guide you on how you can redirect www site traffic to the non-www website without effecting your site traffic.

Prerequisites

Before we start I hope you have a live website hosted on NGINX server and you have complete access to your server from terminal.

Configure NGINX to redirect www traffic to non-www

Step 1. Login to your server via terminal.

Step 2. Now we need to create a new configuration file inside /etc/nginx/sites-available for example, if I want to redirect www.trinitytuts.com traffic to trinitytuts.com I create a new file and name it www.trinitytuts.com.

Now run below command in terminal and add code given below

cd /etc/nginx/site-available

Once the directory is open in the terminal run this command nano www.trinitytuts.com and add below server configuration in it.

server {
  server_name www.trinitytuts.com;
  return 301 https://trinitytuts.com$request_uri;
}

Save the file press ctrl+o and after save ctrl+x.

Step 3. Now once we save the file we need to verify or configuration file to verify run this command nginx -t and everything is fine we need to enable the configuration using the below command.

sudo ln -s /etc/nginx/sites-available/www.trinitytuts.com /etc/nginx/sites-enabled/

Step 4. Restart your server once everything is done.

sudo systemctl restart nginx

If you want to check your site is redirected successfully with the status you can google redirect-checker and check there. Or if you want to check-in terminal run this command curl -I https://www.trinitytuts.com

Redirect www site traffic to non-www website nginx

Note:- you need to replace trinitytuts.com with your website name.