My site is running on apache2 (Ubuntu 18.x)
When I visit either example.com or http://example.com or https://example.com - everything is fine.
But when I enter www.example.com in the address bar, a request is sent to https://www.example.com (can reproduce with curl -I https://www.example.com)
Now this gives me a certificate error:
SSL: no alternative certificate subject name matches target host name 'www.example.com'
It seems the certificate only matches mysite.org and not www.mysite.org. I tried to resolve this by redirecting all traffic that's https://www. to https://
To test my configuration (made in .htaccess) I made a redirection for all https traffic And that works:
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteRule ^(.*)$ https://google.be [R=301,L]
Result:
curl -I https://example.com
HTTP/1.1 301 Moved Permanently
Date: Fri, 23 Sep 2022 08:01:05 GMT
Server: Apache/2.4.41 (Ubuntu)
Location: https://google.be
Content-Type: text/html; charset=iso-8859-1
Yet, when requesting https://www.example.com results in the same error as above! In essence: the certificate error is thrown before the redirection takes place (or it seems like this)
Can anyone explain to me what's going on and how to fix this? I would rather not buy another SSL certificate that includes www.example.com, and use redirection instead.