How to Redirect www to Non-www with Nginx?

Published June 28, 2024

Configuring Nginx to redirect www to non-www

Website owners often use a single version of their domain to improve SEO and user experience. Redirecting the www version of a domain to the non-www version (or vice versa) is common. For Nginx users, setting up this redirection needs specific configuration. This article explains how to redirect www to non-www using Nginx, helping you keep a consistent domain structure for your website.

Setting Up Nginx for www to Non-www Redirection

Creating a Server Block for Redirection

To set up a server block for www to non-www redirection:

  1. Create a new config file:

    sudo nano /etc/nginx/sites-available/
  2. Add this server block:

    server {
       listen 80;
       # Other server configurations go here
  3. Use the server_name directive to specify both www and non-www versions of your domain.

  4. Save the file and exit the text editor.

  5. Create a symbolic link to enable the site:

    sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
  6. Remove the default Nginx config:

    sudo rm /etc/nginx/sites-enabled/default

This setup forms the basis for implementing the www to non-www redirection in Nginx.

Implementing the 301 Redirect in Nginx

Writing the Redirect Rule

To redirect www to non-www using Nginx, use the return directive with a 301 status code. This shows a permanent redirect. Add this rule to your server block:

server {
    listen 80;
    if ($host = {
        return 301 $scheme://$request_uri;
    # Other server block configurations

This rule checks if the requested host is the www version of your domain. If it is, it redirects to the non-www version, keeping the original scheme (http or https) and any URL parameters.

Testing the Nginx Configuration

After writing the redirect rule, test your Nginx configuration for errors:

  1. Run this command:

    sudo nginx -t
  2. If there are no errors, you'll see a message showing the configuration test is successful.

  3. If there are errors, check your configuration file and fix any issues.

Once the configuration test passes, reload Nginx to apply the changes:

sudo systemctl reload nginx

This command reloads Nginx without stopping active connections. Your www to non-www redirection should now work. Test it by visiting your www domain in a web browser to confirm it redirects to the non-www version.

Additional Considerations for Redirection

Handling HTTPS Connections

To handle HTTPS connections, set up SSL certificates and update your Nginx server block:

  1. Get an SSL certificate for your domain.

  2. Install the SSL certificate on your server.

  3. Update your Nginx server block to support HTTPS:

server {
    listen 80;
    listen 443 ssl;

    ssl_certificate /path/to/your/certificate.crt;
    ssl_certificate_key /path/to/your/certificate.key;

    if ($host = {
        return 301$request_uri;

    if ($scheme != "https") {
        return 301 https://$host$request_uri;

    # Other SSL and server configurations

This configuration redirects HTTP and HTTPS traffic from www to non-www, and uses HTTPS for all connections.

DNS Configuration for www and Non-www Domains

To set up DNS for www and non-www domains:

  1. Create an A record for your non-www domain ( pointing to your server's IP address.

  2. Set up a CNAME record for the www subdomain ( pointing to your non-www domain.

Example DNS records:     A CNAME

After making DNS changes, allow time for propagation. This can take up to 48 hours, but often happens within a few hours. Use online DNS lookup tools to check the propagation status of your domain.

By handling HTTPS connections and configuring DNS, you create a secure redirection system for your www and non-www domains.

Troubleshooting Common Redirection Issues

Dealing with Redirect Loops

Redirect loops happen when redirects lead back to the original URL, creating a cycle. To find and fix circular redirects:

  1. Use online redirect checkers or browser developer tools to spot loops.

  2. Check your Nginx config files for conflicting redirect rules.

  3. Look for redirects in your content management system or .htaccess files.

To find redirection problems in server logs:

  1. Access Nginx error logs:

    sudo tail -f /var/log/nginx/error.log
  2. Look for repeated redirection entries or error messages.

  3. Use log analysis tools to find patterns in redirection behavior.

Optimizing Redirection for Search Engines

To help search engines understand your redirects:

  1. Use 301 (permanent) redirects for www to non-www. Check this in your Nginx config:

    return 301 $scheme://$request_uri;
  2. Update your XML sitemap:

    • Remove www URLs
    • Add non-www URLs
    • Submit the updated sitemap to search engines
  3. Change your robots.txt file:

    • Use the non-www version in the Sitemap directive
    • Example:
  4. Use Google Search Console to:

    • Set your preferred domain version
    • Check for crawl errors related to redirects

By fixing redirect loops and optimizing for search engines, you can improve your website's performance and search visibility.