Score:0

Extra trailing slash when re-directing www to non-www

ru flag

This is my current configuration:

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


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

    listen 80;
    server_name mywebsite.com www.mywebsite.com;
    return 404; # managed by Certbot
}

server {
    server_name mywebsite.com;
    root /var/www/html/mywebsite.com/public;

    location / {
        try_files $uri $uri/index.html @sinatra;
    }

    location @sinatra {
        proxy_pass http://127.0.0.1:4567;
    }

    listen [::]:443 ssl http2 ipv6only=on;
    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.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
}

What I want is to have everything... www-version or http-version of any URL redirected to the non-www HTTPS url.

Right now, everything works with a couple of catches:

  • https://mywebsite.com works fine
  • http://mywebsite.com redirects to https://mywebsite.com (perfect too)
  • https://www.mywebsite.com redirects to https://mywebsite.com// (double trailing slash!)
  • http://www.mywebsite.com redirects to https://mywebsite.com// (double trailing slash, again!)

How do I go about fixing this?

Richard Smith avatar
jp flag
Does `www.mywebsite.com` point to this server? The above configuration does not redirect from `https://www.mywebsite.com` to `https://mywebsite.com`.
Michael Hampton avatar
cz flag
Yes, there is definitely a lot of stuff missing that should be here. Please post the output of `nginx -T`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.