remove www from domain URL

How To Remove WWW from Domain URL in NGINX

Last updated on July 4th, 2024 at 06:02 am

Generally, websites are accessible via their www as well as non-www URLs. But web administrators may need to redirect www to non-www URL to remove www from domain URL. This is useful in SEO as it helps tell search engines to treat www and non-www URLs on your website as one, and avoids any penalty. It also helps users remember the home page URLs quickly. In this article we will look at how to remove WWW from domain URL in NGINX.

Why Remove WWW from Domain URL

We all know that Google penalizes duplicate content on a website. When a web page is accessible via both its www as well as non-www URL then search engines may see it as a duplicate content and push down that page’s ranking in search results. Also, it is difficult for users to remember whether your domain and its web pages contains www or not. So it is advisable to maintain only a single version of your web pages – either www or non-www one. Most websites these days drop www from its URLs and redirect all www pages to their non-www pages.

How To Remove WWW from Domain URL in NGINX

Here are the steps to remove WWW from domain URL in NGINX.

1. Backup NGINX Configuration File

Since we need to update NGINX configuration file, it is better to do its backup before you edit it. Open terminal and run the following command for this purpose.

$ sudo cp /etc/nginx/nginx.conf /etc/nginx/backup-nginx.conf

2. Open NGINX configuration file

Open terminal and run the following command to open NGINX server configuration file.

$ sudo vi /etc/nginx/nginx.conf

If you have configured separate virtual hosts for your website (e.g www.example.com), such as /etc/nginx/sites-enabled/website.conf then open its configuration with the following command

$ sudo vi /etc/nginx/sites-enabled/website.conf

Alternatively, you can also open the default virtual host configuration file.

$ sudo vi /etc/nginx/sites-enabled/default

3. Redirect WWW to Non-WWW

You will need to create two server blocks in NGINX configuration – one for www and another for non-www.

In server block for www, we will redirect all URLs to non-www server block.

Here is the full configuration for it. Replace example.com with your domain name.

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

The first server listens to URLs starting with www.example.com and redirects them to URLs starting with example.com, which is handled by the second server. The ‘return 301’ directive does a permanent redirection. $scheme is the URL protocol (HTTP or HTTPS). $request_uri contains the URL stub in original request. Both the URL scheme and request_uri are retained in the redirected request. This is similar to redirecting HTTP to HTTPS pages.

4. Restart NGINX Server

Run the following command to check syntax of your updated config file.

$ sudo nginx -t

If there are no errors, run the following command to restart NGINX server.

$ sudo service nginx reload #debian/ubuntu
$ systemctl restart nginx #redhat/centos

5. Update Canonical Page Tag (Optional)

Most web pages contain canonical tag to tell search engines about the original URL of those pages. In case there are multiple URLs on your website, with the same content, then search engines will use the canonical link’s URL as the original page. Search engines display this canonical URL in search results so it is important. This is common in ecommerce sites where each page can be accessed via multiple URLs.

So after you have redirected a web page from its www URL to its non-www one, it is also important to update the canonical URL tag, if any, in the page’s code. For example, if your canonical tag contains www URL then you need to update it to non-www URL. Let us say you have the following canonical tag on your home page.

<link rel="canonical" href="https://www.example.com" />

You will need to update it to the following.

<link rel="canonical" href="https://example.com" />

If you do not make the above change, then search engines will continue to use the www version of URL as the original URL and show it search results. Alternatively, you can completely remove this tag from your web page. Based on the result of redirection, search engines will automatically deduce the original URL.

Conclusion

Hopefully, this article will help you redirect WWW to Non-WWW URLs in NGINX. Basically, you need to open NGINX virtual host or config file and create two server blocks – one for handling www requests and the other for handling non-www requests. The server handling www requests redirects all received requests to the 2nd server. We have used return statement for URL redirection. NGINX provides several ways such as using rewrite directive for redirecting URLs. You can use any of them depending on your requirement. After setting up redirection, remember to update the canonical tag in your web pages so that search engines clearly understand which is the original URL – www or non-www one.

Ubiq makes it easy to visualize data, and monitor them in real-time dashboards. Try Ubiq for free.

Also read :
How to Redirect WWW to Non-WWW URLs
How to Configure Basic Authentication in NGINX
How to Enable mod_rewrite in XAMPP
Rewrite vs Redirect in NGINX