1

What is the best practice writing simple site config file based on ServerName with HTTPS permanent redirect?

  1. Do I need to check mod_ssl each time I redirect?
  2. Can we just use one common <VirtualHost *:*> block to solve HTTPS redirects?
<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerName example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLCertificateFile /etc/server/fullchain.pem
        SSLCertificateKeyFile /etc/server/privkey.pem
        Include /etc/server/options-ssl-apache.conf
</VirtualHost>
</IfModule>

<VirtualHost *:80>
        ServerName example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <IfModule mod_ssl.c>
                RewriteEngine on
                RewriteCond %{SERVER_NAME} =example.com
                RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
        </IfModule>
</VirtualHost>
kasperd
  • 29,894
  • 16
  • 72
  • 122

0 Answers0