Redirecting www to non-www (apache, letsencrypt )
Solution 1:
Since your SSL certificate is not valid for www.example.com, when someone accesses your site using www.example.com, they will get a certificate warning. This happens regardless of whether they are served a redirect or not. Otherwise, it would be possible to create a redirect to any site at all and it would look trustworthy.
What you need to do is to either get a certificate that covers both www.example.com and example.com, or to set up a separate virtualhost for www.example.com, with a valid certificate, and put the redirect in that virtualhost.
Solution 2:
- You cannot include a redirection http -> https rule inside a SSL-enabled virtualhost. You cannot by design. Just accept it. You need a plain-HTTP virtualhost for doing redirection from HTTP (and this is your main mistake).
- Actually you can have just any config file inside your configuration directory in Debian-based Linux. You can even delete the dogmatic and uncomfortable
sites-*
directories and start usingvhost.d
directory, like I do. Nobody can force you use something that is uncomfortable.