将 www 重定向至非 www 时添加额外的尾部斜杠

将 www 重定向至非 www 时添加额外的尾部斜杠

这是我当前的配置:

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
}

我想要的是将一切...任何 URL 的 www 版本或 http 版本重定向到非 www HTTPS URL。

目前,一切正常,但有几个问题:

  • https://mywebsite.com工作正常
  • http://mywebsite.com重定向至https://mywebsite.com(同样完美)
  • https://www.mywebsite.com重定向至https://mywebsite.com//(双斜杠!)
  • http://www.mywebsite.com重定向至https://mywebsite.com//(再次双斜杠!)

我该如何修复此问题?

相关内容