Report this

What is the reason for this report?

How to propertly redirect www.domain.com to https://domain.com

Posted on April 7, 2022

I am getting duplicate URLs on crawls of my Django site via my SEO tools and via Google tools. I want to ditch www.domain.com completely and redirect all traffic to https://domain.com

Below is my NGINX conf. Cerbot has added its https info. Curious what I’m doing wrong here.

server {


server_name domain.com;

        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
        root /root/domain/domain-django;
        }

location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

    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


}
server {
        server_name www.domain.com;
        return 301 $scheme://domain.com$request_uri;
}
server {

    if ($host = domain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name domain.com;
    return 404; # managed by Certbotserver
}


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

Currently, you don’t have a redirect rule for https://www.domian.com to https://domian.com. The redirects that you have are only for http://.

You can fix that by adding that redirect.

Here is an example:

server {
    listen              80;
    server_name         www.yourdomain.com yourdomain.com;
    return              301 https://yourdomain.com$request_uri;
}

server {
    listen              443 ssl;
    server_name         www.yourdomain.com;
    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
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    return              301 https://yourdomain.com$request_uri;
}

server {
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
        root /root/domain/domain-django;
        }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

    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
}

Hope that this helps!

Best,

Bobby

Heya,

If you want to ensure that all requests to www.domain.com are redirected to https://domain.com and that all non-secure (http) requests are also redirected to their secure (https) counterparts, you’ll need a few modifications to your Nginx configuration.

Let’s break down the issues and correct them:

  1. Redirect www to non-www:

Your configuration for this appears mostly correct. But for clarity, let’s ensure it handles both http and https:

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl;
    server_name www.domain.com;

    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    return 301 https://domain.com$request_uri;
}
  1. Redirect non-secure (http) to secure (https):

Your configuration is using a conditional inside a server block. Using if in Nginx can be problematic in some contexts, so it’s generally avoided when there’s a more straightforward method. Instead, use separate server blocks to handle the redirects more explicitly.

server {
    listen 80;
    server_name domain.com;

    return 301 https://domain.com$request_uri;
}
  1. Main server block for https:

This looks okay. Just make sure that you are only listening on port 443 with SSL for the main domain.

server {
    listen 443 ssl;
    server_name domain.com;

    # ... (rest of your configurations)

    # SSL configurations
    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

With these configurations in place, you should have:

  • All www.domain.com traffic redirected to https://domain.com.
  • All non-secure (http) traffic redirected to its secure (https) counterpart.

After making these changes, don’t forget to test your Nginx configuration:

sudo nginx -t

If there are no errors, reload Nginx to apply the changes:

sudo systemctl reload nginx

Lastly, you might also want to consider setting the canonical meta tag in your HTML to further hint to search engines about the preferred version of the URL.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.