After spending many hours, searching and trying different solutions, either in Cloudfront or in Route53, the solution that @Paramvir has suggested, has finally helped me figure it out.
I ended up using a Cloudfront function, that looks like this:
function handler(event) {
var request = event.request;
console.log(request.headers["host"]);
if (request.headers["host"] && request.headers["host"].value.startsWith("www")) {
var response = {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers: {
'location': { value: 'https://naked_address.com'+event.request.uri }
}
};
return response;
}
return request;
}
What it basically does is that it checks for the request address, if it starts with www, then redirects it to naked address. Otherwise it returns the untouched request.
Of course, don't forget to save your function, then publish it. After you do this, you must associate it to your distribution.