Step-by-step guide to redirect from www to non-www in Nginx
- 2024-09-03 (2 months ago) |
- Syed Shahzaib |
- Linux Guide
Prerequisite for Redirecting from WWW to Non-WWW in Nginx
Before diving into the actual process, I assume that you have already Installed Nginx and are familiar with connecting to your VPS using PuTTY or Terminal .
Disclaimer: Always backup your configuration file!
Step 1 - Back Up the configuration file
Before making any changes, it is highly recommended to back up your existing configuration file, which is usually located in /etc/nginx/conf.d/domain.com.conf
. Run the following command as a sudo user to create a backup:
sudo cp /etc/nginx/conf.d/domain.com.conf /etc/nginx/conf.d/domain.com.bkp
Step 2 - Redirect from WWW to Non-WWW
Setting up redirection in Nginx may seem straightforward, but it can be tricky if not handled correctly. To ensure that requests to www.domain.com
are properly redirected to https://domain.com
, you should separate the handling of www.domain.com
and domain.com
into distinct server blocks. Here’s how you can do it, depending on whether SSL is enabled for your domain:
server
{
# Redirect all requests to https://www.domain.com to https://domain.com
server_name www.domain.com;
listen 80;
listen 443 ssl; # Certbot SSL
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
return 301 https://domain.com$request_uri;
}
In this example, SSL is included, so all relevant lines are kept intact to handle the redirection from www
to non-www
.
Step 3 - Main block
This server block will serve as the entry point for requests to https://domain.com
and will load your site’s files from the specified root directory:
server
{
# Handle requests for https://domain.com
server_name domain.com;
root some_path;
access_log some_path/domain.com_access.log;
error_log some_path/domain.com_error.log;
index index.php index.html index.htm;
## Main public_html nginx conf
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/opt/remi/php81/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
Step 4 - Configuring SSL redirection in Nginx
This block will redirect all HTTP traffic to HTTPS, ensuring that your website is served securely over SSL:
server
{
# Redirect all HTTP traffic to HTTPS
listen 80;
server_name domain.com www.domain.com;
return 301 https://$host$request_uri;
}
Conclusion
Combine all three server blocks and paste them into your Nginx configuration file, typically located in the /etc/nginx/conf.d/
directory on your server. The filename should follow the format domain.com.conf
. To avoid any issues, run the command sudo nginx -t
to test the Nginx configuration before reloading it. This step is crucial to prevent breaking Nginx. Make sure no errors are present (warnings can be ignored). Implementing this redirection is essential for SEO, as it helps maintain a consistent URL structure, which might otherwise be difficult to manage. I hope you found this guide helpful—stay tuned for more content like this!