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?