I host a bunch of client websites on a CentOS 7 server using Apache 2.4. I am setting up my client websites with SSL using Let’s Encrypt. I also create a seperate .conf file for each client website to make it easier to manage them individually. Now that I’m using SSL, I want to redirect non-SSL domains to the https version. I also want to redirect www.domain.com
to just domain.com as well. So this is my .conf file for an individual client website:
<VirtualHost 999.999.999.999:443>
ServerName clientdomain.com
DocumentRoot /home/websites/3456/www/
ServerAdmin webmaster@mysite.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/clientdomain.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/clientdomain.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/clientdomain.com/fullchain.pem
<Directory "/home/websites/3456/www/">
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
# -- SSL Redirect of www to plain domain
<VirtualHost 999.999.999.999:443>
ServerName www.clientdomain.com
Redirect permanent / https://clientdomain.com/
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/clientdomain.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/clientdomain.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/clientdomain.com/fullchain.pem
</VirtualHost>
# -- Non-SSL Redirect to SSL version of website
<VirtualHost 999.999.999.999:80>
ServerName clientdomain.com
ServerAlias www.clientdomain.com
Redirect permanent / https://clientdomain.com/
</VirtualHost>
I’m just wondering if there’s a way to improve this and make it more efficient. I can’t do a “catch-all” solution for redirecting http to https because I have some sites that will not use an SSL certificate for now, so I have to do it on an individual basis. Also, the SSL certificates cover both the plain domain and the www version, which was necessary to do the encrypted redirect and not get warnings from the browser.
Any suggestions on how to improve this, if it can be, are appreciated!