I have one Apache server with many VirtualHosts in various conf files. I thought I could define a blanket rule to redirect all traffic to all of those hosts to HTTPS and non-www urls.
Unfortunately, the configuration below (which I have in my 000-default.conf
file) only partially works. It works for urls that start with http://www.domain, https://domain (no www) and https://www.domain. It does not work for those that start with http://domain. Apache throws a 403 Forbidden (no access to / on this server).
As a bonus, the redirects also add an extra trailing slash that I'd like removed.
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/domain/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/domain/chain.pem
</VirtualHost>
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.?(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,NC]
</VirtualHost>