How to Remove WWW from domain URL in NGINX


Remove WWW from domain URL in NGINX

Google treats the www and non-www versions of your web pages as separate content. So if you don’t consolidate them, they will be seen as duplicate content and this will negatively affect your SEO. If the non-www version of your website has more backlinks, then here’s how to www from domain URL in NGINX and send web traffic to the non-www version of your website.

 

How to Remove WWW from domain URL in NGINX

Here are the steps to remove www from domain URL in NGINX. In Apache, you need to enable mod_rewrite and then write RewriteCond directives to do this, which can be tedious.

On the other hand, it’s quite easy in NGINX.

 

1. Open NGINX server config

Open your NGINX server config file using a text editor.

By default, NGINX error log is located at log/error.log. You will also find it at /var/log/nginx/ on some Linux distributions.


$ sudo vim /var/log/nginx/nginx.conf

 

2. Update NGINX Config file

Add the following line (in bold) to the server block. Replace example.com with your domain name


server {
 server_name www.example.com;
  return 301 http://example.com$request_uri;
}

 

3. Reload NGINX web server

Reload NGINX web server to apply changes


$ sudo service nginx reload

 

That’s it! Now NGINX will automatically remove www from domain URL

 

On the other hand, if you want to add www to your domain URL, then add the following code in NGINX server config file. Replace example.com with your domain name


server {
 server_name example.com;
 return 301 http://www.example.com$request_uri;
}

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!