How To Redirect www To Non-www Domain on Nginx
In this tutorial, we will show you how to redirect www to non-www on Nginx. For those of you who didn’t know, In the world of web development, it is common to have both the www and non-www versions of a website available for access. However, having multiple versions of a website can cause issues with search engines, as it can lead to duplicate content. This can harm the website’s search engine ranking and result in a negative user experience. To avoid this issue, it is recommended to redirect all requests from the www version of a website to the non-www version. In this article, we’ll discuss how to redirect www to non-www in Nginx.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step setup configuration to redirect www to non-www on the Nginx web server.
Prerequisites
- A server running one of the following operating systems: Debian-based or RHEL-based
- SSH access to the server (or just open Terminal if you’re on a desktop).
- An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for Nginx.
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Redirect www To Non-www Domain on Nginx
Step 1. First, make sure that all your system packages are up-to-date by running the following dnf
commands in the terminal.
sudo dnf update sudo dnf upgrade
Step 2. Installing Nginx.
Nginx is available in the official Rocky Linux or RHEL-based repository and can be easily installed using the dnf
package manager. To install Nginx, run the following command:
sudo dnf install nginx
Once the installation is complete, start the Nginx service and enable it to start automatically on boot by running the following commands:
sudo systemctl start nginx sudo systemctl enable nginx
For additional resources on installing Nginx, read the post below:
Step 3. How to Redirect from www to non-www on Nginx.
The process of redirecting from www
to non-www
on Nginx is relatively simple and straightforward. In this section, we will go through the steps to set up the redirect.
The first step in the process is to open the Nginx configuration file. This file is typically located in the /etc/nginx
directory. If you are using a different directory, please make sure to modify the file path accordingly.
Once you have opened the Nginx configuration file, you need to create a new server block. The server block is responsible for handling incoming requests to your website and returning the appropriate response. In this case, the server block will be responsible for redirecting requests from www
to non-www
.
server { listen 80; server_name www.your-domain.com; return 301 $scheme://your-domain.com$request_uri; }
In the above code, listen
a directive is used to specify the port that the server should listen on. In this case, it is set to 80
, which is the default HTTP port. The server_name
directive is used to specify the domain name that this server block should handle. In this case, it is set to www.your-domain.com
. Finally, the return
a directive is used to specify the redirect location. In this case, it is set to the root domain your-domain.com
followed by the request URI.
Finally, save and close the configuration file then restart the Nginx service to make the changes take effect using the following command:
nginx -t sudo systemctl restart nginx
Step 4. Test the Redirection.
To test the redirection, you can use a tool like curl
, which allows you to send HTTP requests from the command line:
curl -I http://your-domain/
Or test the redirection by visiting the www version of the website. The browser should automatically redirect to the non-www version of the website.
Congratulations! You have successfully the configuration to redirect www to non-www on the Nginx. For additional help or useful information, we recommend you check the Nginx website.