How to redirect www to non www traffic on Nginx

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how redirect www to non www traffic on Nginx. This is done to avoid duplicate content issue because google sees www and non www as two different domains when you serve same content.  

However, this change does not benefit to seo only, it does also make your website address look cleaner and easy to remember by your users.

Step 1 - Open nginx.conf with your text editor

nginx.conf is the file where we are going to setup the redirection of www traffic to non www, run the command below to open nginx.conf with the text editor of your choice.

We are using nano, you can use anything you want simply replace nano

sudo nano /etc/nginx/nginx.conf

nginx.conf is the default config file, if you currently have a site on your server then the file that you will need to make the changes to is located at

/etc/nginx/sites-enabled

Inside site-enabled folder should be two conf files, assuming you have a working configuration for http and https traffic.

For example in our case we have owlhowto.com.conf and owlhowto.com-ssl.conf

to edit those files do

sudo nano /etc/nginx/owlhowto.com.conf
sudo nano /etc/nginx/owlhowto.com-ssl.conf

Step 2 - Setup Server block

Before you make changes on your current configuration, please backup nginx configuration files first, you can do this by saving a copy of nginx.conf or other configuration files located at /etc/nginx/sites-enabled, this step is necessary if you have a production site running, so to avoid any downtime because of misconfiguration simply save a copy of those files on your computer so you can undo changes in case something happens.

Let's start now by adding the server block on nginx conf file

Assuming you don't have a redirect setup on your server, nginx.conf should look like this

server {
    listen 80;
    listen [::]:80;
    server_name owlhowto.com;

}

Now for the redirect to work you need to have two server blocks, one to redirect the www traffic to non www, and another one for the non www traffic

Simply add this server block under your current configuration

Redirect www traffic to non www traffic over HTTP

#Server block to redirect www traffic
server {
    listen       80;
    server_name  www.owlhowto.com;
    return       301 http://owlhowto.com$request_uri;
}


#Current Server block
server {
    listen 80;
    listen [::]:80;

    server_name owlhowto.com;
}
/etc/nginx/sites-enabled/owlhowto.com.conf

Redirect www traffic to non www traffic over HTTPS

#Server block to redirect www traffic
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name www.owlhowto.com;
    return 301 https://owlhowto.com$request_uri;
    }

#Current Server block 
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name owlhowto.com;
    
    }

/etc/nginx/sites-enabled/owlhowto.com-ssl.conf

In the examples above we are using two separate server blocks to redirect the traffic, you can also use only one server block by setting two domains www.owlhowto.com and owlhowto.com on one server_name directive

Example

server {
    listen 80;
    listen [::]:80;

    server_name owlhowto.com www.owlhowto.com;
}
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name owlhowto.com www.owlhowto.com;
    return 301 https://owlhowto.com$request_uri;
    }

Replace server_name on both server blocks www.owlhowto.com with your website url and the return url http://owlhowto.com , once you are done press CTRL + X to save this change, do the same for both files owlhowto.com.conf, owlhowto.com-ssl.conf

Step 3 - Check Nginx Syntax

After making changes on nginx.conf run the command below to check if there's an error on the changes you made

nginx -t

Example Output

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Step 4 - Restart Nginx

Proceed to restart nginx if no issues encountered

sudo service nginx restart

Step 5 - Test redirect

All you have to do now is to check if the redirect is working as it should. Try to go to www version of your website and it should redirect you to the version without www.

If you don't have a browser to perform this test, you can use curl

curl -I www.owlhowto.com

Output

owlhowto@owlhowto:~$ curl -I www.owlhowto.com
HTTP/1.1 301 Moved Permanently
Date: Wed, 29 Mar 2023 17:33:37 GMT
Connection: keep-alive
Cache-Control: max-age=3600
Expires: Wed, 29 Mar 2023 18:33:37 GMT
Location: https://owlhowto.com/
Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=D3abkvAf4kmKK%2FVlZn%2Bu%2FIzlQ2J2AeE8JKmk6qZ5tfhuCHmd0IcYVaWLFD%2FDsgH2rj1ovxDsieioPES9As5owRyyW5tOEHt5POeGutXVTp3TjB7dI6hUJnEJgN%2BCWZWYjJS%2B"}],"group":"cf-nel","max_age":604800}
NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
X-Content-Type-Options: nosniff
Server: cloudflare
CF-RAY: 7af9dbe7cfc7380d-FRA
alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400

If the redirect doesn't work, make sure to clear cache before trying again.

Conclusion

In this tutorial you learned how to redirect your www traffic to non www traffic on your server. Even though this is not the only way to do this, this is important to know when you don't want to rely on a third party solution to achieve this. Hopefully this tutorial helped you.