How can I make all www traffic get routed to my domain?


#1

I have two DNS entries; wwww.example.com and example.com I want to make sure every request gets routed to https://example.com. When I ran certbot, it updated my configuration.

However when I browse to http://www.example.com or https://www.example.com it doesn’t get routed to https://example.com/ I get a similar response when I add a path, eg http://www.example.com/foobar.

What do I need to do in order to get all www instances to redirect to https://example.com/path

    server {
    
            root /var/www/example.com/html;
            index index.html index.htm index.nginx-debian.html;
    
            server_name example.com www.example.com;
    
            location / {
                    try_files $uri $uri/ =404;
            }
    
        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/example.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 {
        if ($host = www.example.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = example.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
            listen 80;
            listen [::]:80;
    
            server_name example.com www.example.com;
        return 404; # managed by Certbot
    
    }

#2

Hi @WhiskerBiscuit,

With below conf you will get:

http://example.com -> https://example.com
http://www.example.com -> https://example.com
https://www.example.com -> https://example.com
https://example.com -> No redirection

Conf for example.com:

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

server {
    server_name www.example.com;
    listen [::]:443 ssl;
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.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://example.com$request_uri;
}

server {
    root /var/www/example.com/html;
    index index.html index.htm index.nginx-debian.html;
    server_name example.com;
    location / {
            try_files $uri $uri/ =404;
    }
    listen [::]:443 ssl;
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.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
}

Cheers,
sahsanu


#4

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.