Introduction
Many website developers need to allow users to access their website or applications through the www subdomain and root domain (not www). In other words, the user is visitingand
You should have the same experience. While there are many ways to set it up, the most favorable solution for SEO is to choose your favorite domain name (subdomain or root domain name) and have the web server redirect users who access another domain name to the preferred domain name.
There are many types of HTTP redirects, but in this case it is better to use 301 redirect, which tells the client: "The website you requested has been moved permanently to another URL, go there." Once the browser receives the HTTP 301 response code from the server, it sends a second request to the server, and the user will see the website, probably not noticed that they are being redirected at all.
Why not configure your web server to provide the same website for requests for two domains? This may seem easier, but it doesn't have the SEO advantage of 301 redirection. Permanent redirects tell search engines that your website has a canonical location and improves the search ranking of that URL.
In this tutorial, you will use Nginx to configure 301 redirection on CentOS 7. If you are using Apache instead of Nginx, see this tutorial: How to Redirect www to non-www using Apache on CentOS 7.
Prerequisites
To complete this tutorial, you first need:
- Have superuser permissions on servers running Nginx (
wheel
Users in the group). If you haven't set it up yet, follow this tutorial: Initial Server Setup on CentOS 7. - Nginx is installed and configured to provide your website. Follow this tutorial: How to Install Nginx on CentOS 7.
- Registered domain name. If you don't already have a domain name, you can get a free domain name from Freenom. You can use any DNS provider you prefer (including your registrar) to host your domain name record, just make sure to point the registrar to your provider's domain name server. If you choose to use DigitalOcean DNS, the articles in our documentation show how to do it.
Let's start configuring your DNS records.
Step 1 — Configure DNS Recording
First, you need toand
Point to the server running Nginx. (This tutorial assumes that your domain name is
. When you see the domain name below, replace it with your own domain name. ) You can do this by creating a DNS A record for each name that points to the IP address of your Nginx server.
Open your DNS provider's web console. This tutorial uses DigitalOcean DNS.
In the Add Domain Name form, enter the domain name you registered and click Add Domain Name. This opens the page for the new domain name, where you can view, add, and delete records for the domain name.
Under Create New Record, enter "@" into the "Host Name" text field. This is a special character that means you are adding a record to the root domain name, i.e. onlyAdd records. In the Will Point to text field, enter the public IPv4 address of the server and click Create record (no TTL required).
For the second DNS record, you can use the CNAME record instead of the A record. A CNAME record is an alias pointing to another name instead of an IP address. You can create a CNAME record that willPoint to
, and any HTTP request to the www subdomain will find your server because you just created an A record for the root domain. But for simplicity, just create another A record similar to the first, enter www into the Host Name field, and enter the public IP address of the server into the Will Point field.
After creating two records, it should look like this:
Required A record
After putting these two records,and
The web request should be able to reach your Nginx server. Now let's configure the server.
Step 2 — Configure redirection in Nginx
As stated in the prerequisites, you should have configured your website in Nginx. Site'sserver
The block appears in the main/etc/nginx/
It doesn't matter in the file or in your own file. What's important is that you have configured someserver
block, and theserver_name
The command is set toand/or
. No matter yours
server_name
Containing one or two names is now the name that determines which name you want to be the only hosted site.
Open a file containing your website configuration (e.g./etc/nginx//
)existvi
Or in the editor you like (if you prefer, you can useyum install nano
) and findserver_name
Instructions:
sudo vi /etc/nginx//
server { . . . server_name . . . }
If you want toRedirect to
, please
server_name
Delete from the line, then save and exit the file. (If you want to
Redirect to
, please delete
。)
Then, create a name called/etc/nginx//
(or/etc/nginx//
, if the name you are redirecting is this) the new Nginx configuration file. File names can be named as they like, but like all Nginx configuration files, the file names must be.conf
Ending:
sudo vi /etc/nginx//
Add the following to the fileserver
block, willReplace with your own domain name:
server { server_name ; return 301 $scheme://$request_uri; }
If you want toRedirect to the www subdomain, just in
server_name
Only put in, and put it in the URL of the next line
。
Save and exit when finished.
Before applying the changes, check that your Nginx configuration is free of errors:
sudo nginx -t
Unless you have a syntax error (for example, you forgot a semicolon), the configuration should be correct.
nginx: the configuration file /etc/nginx/ syntax is ok nginx: configuration file /etc/nginx/ test is successful
Now restart Nginx to apply the new redirect rule:
sudo systemctl restart nginx
Visit in the browserPreviously, it could be on the server or on the local machine (if locally installed
curl
)usecurl
Make a request:
curl -IL
-I
Sign tellcurl
Only the headers from the server response are displayed.-L
Sign tellcurl
Follow any redirection of the server, by automatically issuing a second request, the URL of this request is inLocation
The URL given in the header (as a web browser would do). Since you have configured 301 redirects,curl
Two requests should be made, and you should only see the headers for the two responses:
HTTP/1.1 301 Moved Permanently Server: nginx/1.20.1 Date: Thu, 08 Dec 2022 19:24:44 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive Location: HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Thu, 08 Dec 2022 19:24:44 GMT Content-Type: text/html Content-Length: 57 Last-Modified: Thu, 01 Dec 2022 22:10:57 GMT Connection: keep-alive ETag: "63892671-39" Accept-Ranges: bytes
In the rightIn the 301 (permanently moved) response of the original request, please note the last header:
Location:
. The second response comes fromcurl
rightIf your website is normal, the server should have responded with 200 (OK).
Finally, visit in your web browser. In the blink of an eye, you may have missed the redirect. Your website should show up as usual, but again, checking the address bar and noting that "www" is missing in the URL. Most users won't notice this, so they will be like requests
Have the same experience.
in conclusion
In this tutorial, you add two DNS records to your website and configure Nginx to redirect the secondary domain to the preferred domain. Your website can now be accessed through these two domains. Perhaps your website is already directly accessible through these two domains before you read this tutorial. But by simply adding four more lines of Nginx configuration, you have improved your website’s position in search engines, exposing it to more users on the internet.
Want to know more about how Nginx decides which oneserver
Will the block process a specific request? Please consult this guide: Understanding Nginx server and location block selection algorithms.
Summarize
This is all about this article about using Nginx to redirect www to non-www on CentOS 7. For more related CentOS7 Nginx to redirect www to non-www, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!