When you have a website or application up and running Nginx, it is desirable to allow visitors to access the domain using both www and non-www versions of your domain name. However, in today’s age of Search Engine Optimization and users wanting a fast and easy browsing experience, having two URL links can negatively affect the overall experience of your website. However, this doesn’t mean you should abandon one of your visitors’ ways to access the site. Instead, setting up a simple redirection can improve your website’s visitor experience, increase backlink recognition more quickly, and improve SEO rating.
In the below guide, you will learn using how to redirect a www URL to non-www, e.g. (www.example.com) to (example.com) and vice versa with a redirect is called a Permanent Redirect, or “301 redirects”, This can be done on any operating system using Nginx, the examples are for the server blocks only. They do not explain how to set these up or how to install Nginx.
Table of Contents
Prerequisites
- Recommended OS: Any Linux system that can run Nginx
- User account: A user account with sudo or root access.
- Recommended Packages: curl
Install curl Centos/Rocky Linux/Rhel/Oracle:
sudo yum install curl
Install curl Debian/Ubuntu/Linux Mint:
sudo apt install curl
Option 1: Redirect NON-WWW to WWW
In the first example, you will add the below code above your existing (www) server name block to redirect all visitors that hit your non-www URL to a www URL only.
HTTP (80)
server {
server_name .example.com;
listen 80;
listen [::]:80;
return 301 https://www.example.com$request_uri;
}
HTTPS (443)
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name .example.com;
# SSL
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
ssl_trusted_certificate /path/to/cert.crt;
return 301 https://www.example.com$request_uri;
}
Note, make sure ($request_uri) is not removed as this can cause problems with all non-www links just redirecting back to your home page.
An example of this is https://example.com/random-topic. If the ($request_uri) is not added, it will simply divert to www.example.com instead of https://www.example.com/random-topic, which will frustrate your visitors plus hurt your search engine ranking with crawlers getting confused.
Option 2: Redirect WWW to NON-WWW
In the second example, you will add the below code above your existing (non-www) server name block to redirect all visitors that hit your www URL to a non-www URL only.
HTTP (80)
server {
server_name www.example.com;
listen 80;
listen [::]:80;
return 301 https://example.com$request_uri;
}
HTTPS (443)
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.example.com;
# SSL
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
ssl_trusted_certificate /path/to/cert.crt;
return 301 https://example.com$request_uri;
}
As noted at the end of Option 1, make sure ($request_uri) is not removed. This is an essential part.
Note, make sure to place the certificates and key for the HTTPS redirects or have issues.
Confirm Changes
Before you restart your Nginx instance, it’s best to do a dry run to ensure no errors in what you just added. To test, execute the following command:
sudo nginx -t
If everything is ok, you should get the following output:
nginx: the configuration file /etc/nginx/my-server.conf syntax is ok
nginx: configuration file /etc/nginx/my-server.conf test is successful
Proceed to restart your Nginx service to make the changes live:
sudo systemctl restart nginx
That’s it! You have completed the redirection. To test if the redirection is working, execute the following:
non-www to www redirection test
curl -I https://example.com
Example output:
HTTP/1.1 301 Moved Permanently
Server: nginx/1.21.1 (Ubuntu)
Date: Mon, 14 July 2021 18:20:19 GMT
Content-Type: text/html
Content-Length: 193
Connection: keep-alive
Location: http://www.example.com/
www to non-www redirection test
curl -I https://www.example.com
Example output:
HTTP/1.1 301 Moved Permanently
Server: nginx/1.21.1 (Ubuntu)
Date: Mon, 14 July 2021 18:21:33 GMT
Content-Type: text/html
Content-Length: 193
Connection: keep-alive
Location: http://example.com/
Comments and Conclusion
In the guide, you have learned how to easily and quickly add both www and non-www direction to your Nginx’s server site block, which will improve the overall experience of your visitors by only visiting one URL instead of two.
As explained at the start of the guide, SEO is becoming more crucial, search engines are constantly marking down websites, and having two URLs with potential duplicate content is a big red mark against your website. Adding this redirection can quickly boost your ranking and your backlinks.