I saw many solutions nothing is working for me. I am trying to redirect the user from www to non www url. Here is the server code I am using
server {
if ($host = https://www.example.com.com) {
return 301 https://example.com.com$request_uri; // IT DOESN'T WORK
} # managed by Certbot
if ($host = www.example.com.com) {
return 301 https://example.com.com$request_uri;
} # managed by Certbot
if ($host = example.com.com) {
return 301 https://example.com.com$request_uri;
} # managed by Certbot
listen 80;
server_name example.com.com www.example.com.com;
return 404; # managed by Certbot
}