Redirecting from www to non-www with Nginx (CentOS 7)
What will you learn in this guide?
In this guide, you will learn how to configure www and non-www domain redirection in an SEO friendly way on Nginx servers running on CentOS 7.
With 301 permanent redirects, you will set a single canonical domain name and maintain your search engine score.
1. Verify DNS records
Both domains must reach the server before the redirect works.
Make sure the following A records point to the server IP address:
@→ Server IP addresswww→ Server IP address
This setting ensures that both addresses reach Nginx.
2. Create Nginx redirect block
In this example, www.site.com → site.com is redirected.
Open the main configuration file:
sudo vi /etc/nginx/conf.d/site.com.conf
- Leave only the actual domain name in this file:
server {
server_name site.com;
# Diğer site ayarları
}
- Now create a new file for redirect:
sudo vi /etc/nginx/conf.d/www.site.com.conf
- Let the content be as follows:
server {
server_name www.site.com;
return 301 $scheme://site.com$request_uri;
}
- This structure permanently redirects all requests coming via www to the main domain.
3. Test and apply configuration
Check the syntax first:
sudo nginx -t
- This command checks for Nginx configuration errors.
- Then restart the service:
sudo systemctl restart nginx
- This action activates the new routing rule.
4. Verify the redirect
- Check HTTP headers via terminal:
curl -IL http://www.site.com
-
The first response should be 301 Moved Permanently, the second response should be 200 OK.
-
This result indicates that the redirect is working correctly.
Frequently Asked Questions (FAQ)
1. Why is 301 redirection important? It notifies search engines of the permanent address and concentrates SEO power in a single address.
2. Why should I use 301 instead of 302? 302 is temporary. SEO value is not fully transferred.
3. What changes if I use HTTPS? The same structure is applied in SSL blocks with listen 443 ssl.
4. Does this redirect slow down the site? No. Nginx processes redirects in milliseconds.
A properly configured Nginx redirect is critical for SEO consistency and user experience. You can immediately evaluate GenixNode solutions for high-performance infrastructures where you can apply such settings without any problems.

