Redirect HTTP to HTTPS on Apache2 or Apache?

Is your website not accessible on http://domain… One of the root cause is when a new SSL certificate is installed on the server. After adding the SSL certificate, we have to manually redirect the http requests to https. The easiest and safest way to do this is by using the Redirect syntax of the Apache in the Virtual Host file. This can be located in the Apache folder.

Two Virtual Host files are required on Apache, one on port 80 which is non-secure and other on port 443 which is secured. The Redirect happens in the port 80 and the overall configuration of both the files will be as shown:

<VirtualHost *:80>
	ServerName domainname.com
	Redirect permanent / https://domainname.com/
</VirtualHost>

<VirtualHost *:443>
	ServerAdmin myemailid@domain.com
	ServerName domainname.com
	DocumentRoot /usr/local/apache2/htdocs
        #   SSL Engine Switch:
	#   Enable/Disable SSL for this virtual host.
	SSLEngine on
        ...
</VirtualHost>

This will permanently redirect all the pages on http to https. The above solution is for linux based systems. The last step is to restart or reload the Apache web server as shown:


/etc/init.d/httpd restart

Tried on Apache 2.4.6.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.