Home > database >  Redirecting all non-www links to www using nginx
Redirecting all non-www links to www using nginx

Time:10-01

I’ve seen what some other posts do but replicating them doesn’t seem to work in my scenario. I’m trying to redirect all non-www links to www (specifically https-www), so if someone loads http://example.com or https://example.com, it'll redirect them to specifically https://www.example.com.

One post suggested a new server block as follows:

server {
        listen 80;
    server_name example.com

    return 301 https://www.example.com$request_uri;
}

But this doesn’t redirect anything. Presumably this should apply when visiting http://example.com.

I also tried changing the http to https redirects generated by certbot - from this:

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

To this:

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

if ($host = example.com) {
    return 301 https://www.$host$request_uri; # changed - added www
} # managed by Certbot

But this also didn’t work. I’d have thought adding www. before $host should prepend www to all http://example.com links, but this doesn't.

My full file (/etc/nginx/sites-available/example.com (symlinked to /sites-enabled):

server {
    root /var/www/example.com;

    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
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
}

Can anyone advise me what I’m doing wrong, and what would fix the non-www http and https redirects properly?

Thank you.

UPDATE:

Still not working after being advised to follow the advice of this post - I want to redirect to www.example.com from example.com but presumed the same principles apply. Any advice would be appreciated.

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

server {
    listen 80 default_server;
    listen 443 ssl default_server;
    return 301 $scheme://www.example.com$request_uri;
}

server {
    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl;
    server_name example.com www.example.com;
    root /var/www/example.com;
    index index.html index.htm index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }

    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
    listen 80;
    listen [::]:80;

    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    }

    if ($host = example.com) {
        return 301 https://$host$request_uri;
    }

    server_name example.com www.example.com;
    return 404;
}

CodePudding user response:

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

This should work... probably a stupid question, but have you restarted nginx since adding this to the config?

failing this, you could set up a redirection at dns level, from example.com to www.example.com with a 301.

CodePudding user response:

Readed from this docs

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

server {
    listen       80;
    server_name  www.example.com;
    ...
}
  • Related