I have a example that should always result in https://example.com, even when the user is entering www.example.com, https or not. Ideally, the automated letsencrypt config https config file should work.
- I have tried to remove the A-record for www and use a CNAME instead to point to example.com or @. This does not seem to have any effect and users still end up on www. I have no idea why.
This is my current config:
<VirtualHost [IPV6]:80 IPV4:80>
ServerAdmin webmaster@example.com
DocumentRoot /home/example/public_html
ServerName example.com
ServerAlias www.example.com
ErrorLog logs/example.error.log
CustomLog logs/example.acccess.log common
<Directory /home/example/public_html>
Options FollowSymLinks
AllowOverride All
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://example.com%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost [IPV6]:443 IPV4:443>
ServerAdmin webmaster@example.com
DocumentRoot /home/example/public_html
ServerName example.com
ServerAlias www.example.com
ErrorLog logs/example.error.log
CustomLog logs/example.acccess.log common
<Directory /home/example/public_html>
Options FollowSymLinks
AllowOverride All
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>
Then however, https://www.example.com throws a bad certificate, even if I add this to the :443 config:
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://example.com%{REQUEST_URI} [END,NE,R=permanent]
How can I redirect http AND https requests to www.example.com to https://example.com?