I have an Nginx web server and I need to:
- Redirect all HTTP (both www and non-www) to HTTPS
- Redirect all HTTPS non-www to www
I know that I can use different server{} blocks but I want to achieve this with one server block using "if" statements, so here is my actual Nginx vhost configuration:
server {
listen 80;
listen 443 ssl http2;
server_name website.com www.website.com;
if ($scheme = "http") {
return 301 https://www.website.com$request_uri;
}
if ($host = "website.com") {
return 301 https://www.website.com$request_uri;
}
[...]
}
It works fine without problems:
http://website.com => https://www.website.com
http://www.website.com => https://www.website.com
https://website.com => https://www.website.com
My questions is if my configuration is correct.
Thanks!