Apache virtual host entry to forward non-SSL to SSL for Rails application

In the rails application, if we need to forward all non-SSL traffic to SSL traffic we can use below virtual host entry.

By default, the rewrite module needs to be enabled in Apache configuration with valid SSL certificates including Chain file.

<VirtualHost *:80>
ServerName mydomain.com
ServerAlias files.mydomain.com
ServerAdmin admin@mydomain.com
DocumentRoot /var/www/html
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteCond %{REQUEST_URI} !^/health_check
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
ErrorLog /var/log/apache2/errors.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
SetEnv RAILS_ENV production
<Directory /var/www/html
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
<VirtualHost *:443>
ServerName mydomain.com
ServerAlias files.mydomain.com
ServerAdmin admin@mydomain.com
DocumentRoot /var/www/html
ErrorLog /var/log/apache2/errors.log
LogLevel warn
CustomLog /var/log/apache2/ssl_access.log combined
SSLEngine on
BrowserMatch "MSIE [2-6]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
# the following ssl certificate files are generated for you from the ssl-cert package.
SSLCertificateFile /etc/apache2/ssl/ssl_certificate.crt
SSLCertificateKeyFile /etc/apache2/ssl/certificate.key
SSLCertificateChainFile /etc/apache2/ssl/ssl_certificate-bundle.crt
SetEnv RAILS_ENV production
<Directory /var/www/html>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>

Above VirtualHost entry will forward all non-SSL to SSL.

Leave a Comment

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

We promise not to spam you, and we don't usually send more than one email a week.

You have Successfully Subscribed!