Introduction


Once you get your domain, you point it to the corresponding naked domain. In some situations, you need to direct your customers to the same content or web application through the "www." subdomain. Doing this, will avoid taking your customer to a 404 error page if they point the non naked domain in their browser's address bar.

The best scenario is to let your audience to visit your domain with or without the "www." prefix, e.g. example.com or www.example.com. The most efficient and practical approach to accomplish this, considering SEO best practices is to choose which domain you prefer, naked or www, and redirect the other one to the preferred domain. This type of domain redirection is called a Permanent Redirect, or "301 redirect", and can be easily set up by properly configuring your DNS records and the web server installed on your dedicated or VPS server.

In this tutorial you will learn how to redirect a www URL to non-www, e.g. www.example.com to example.com, with Nginx on CentOS 7.

Prerequisites


In this guide we will assume that you have superuser privileges (sudo or root), on the dedicated or VPS server running Nginx. The web server should also be installed for us to be able to proceed.

It is imperative that you be able to modify your DNS records with your domain registrar or DNS provider.

First, you will need to setup your DNS records and point your domain to the corresponding dedicated or vps server public IP address.

Configure DNS Records


In this case, you will need to modify the A records for your domain and include both, the naked and the www subdomains to the DNS zone file. For this guide we will use www.example.com to example.com.

Login and open your DNS manager console.

You will see a DNS record table like this:


HOST NAME		IP ADDRESS/URL		RECORD TYPE		MX PREF		TTL 
@						A(Address)		N/A			1800
www						CNAME(Alias)	N/A			1800

You should create the domain/zone record if it does not already exist. The HOSTNAME (@) should be your domain, e.g. example.com, and the IP ADDRESS/URL should be set to the public IP address of your Nginx web server, typicaly your dedicated or VPS server's IP address. This will create an A record that points your domain to the IP address that you specified. The MX and TTL values are generally auto populated for most DNS providers. If not, 1800 seconds TTL is a good standard for DNS query/propagation.

When you have created both records, it should look like this:


HOST NAME		IP ADDRESS/URL		RECORD TYPE		MX PREF		TTL 
@			123.123.123.123		A(Address)		N/A			1800
www			123.123.123.123		CNAME(Alias)	N/A			1800

At this point, your IP address is accessible through both, the www sub-domain and the non-www naked domain. But we still need to tell the web server how to treat both the www and the non-www requests to server the same content or web application.

Configure Nginx Redirect


In order to perform the 301 redirect, you must add a new Nginx server block that points to your original server block.

Open your Nginx server block configuration in the editor of your choice. In this case we will use the vi editor. We'll add another configuration file in the Nginx include directory, /etc/nginx/conf.d called redirect.conf:

sudo vi /etc/nginx/conf.d/redirect.conf

You should see the original server block already defined. In order to redirect from www to non-www domain, you need to use the following syntax/block.


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

Save and exit.

This teaches Nginx web server to redirect requests to www.example.com to example.com. It is important to make sure that the non-www web server block is already specified.

To commit the changes you need to restart Nginx:

sudo systemctl restart nginx

You should be awarethat if you are using HTTPS, the listen directive should be set to port 443 instead of 80.

You can issue this curl command to confirm that the www domain redirects to the non-www domain:

curl -I www.example.com
You should get a 301 Moved Permanently response, that shows the non-wwwredirect location, like this:
HTTP/1.1 301 Moved Permanently
Date: Tue, 26 May 2015 21:50:33 GMT
Server: Nginx
Location: http://example.com/
Vary: Accept-Encoding
Content-Type: text/html; charset=iso-8859-1

Now, you should be able to enter the www version of the domain in the address bar of your web browser and it will provide the website content with the non-www domain as its final destination.

You can also access your domain in a web browser (www and non-www) to confirm correct functionality.

Conclusion


That's it there is to it! Your Nginx permanent redirect is now configured properly on your dedicated or VPS server. Now every time a user access your website through the www or non-www domain, they will be taken to the same content.

 

Test on a Miami VPS Now

or

Deploy on a Miami Dedicated Server

Was this answer helpful? 3 Users Found This Useful (9 Votes)