The best way to do a redirect from HTTP to HTTPS site is to use the Redirect directive in the Apache virtual host configuration. If you have a site configured at https://sample.com
and if you want to redirect the http://sample.com
requests to the https site it can be achieved as follows:
<VirtualHost *:80> | |
ServerName sample.com | |
Redirect permanent / https://sample.com/ | |
</VirtualHost> | |
<IfModule mod_ssl.c> | |
<VirtualHost _default_:443> | |
ServerAdmin admin@localhost | |
ServerName sample.com | |
ServerAlias www.sample.com | |
DocumentRoot /var/www/html/sample.com | |
ErrorLog ${APACHE_LOG_DIR}/sample-site-error.log | |
CustomLog ${APACHE_LOG_DIR}/sample-site-access.log combined | |
SSLEngine on | |
SSLCertificateFile <Path_to_SSL_Certificate> | |
SSLCertificateKeyFile <Path_to_SSL_Key> | |
SSLCACertificateFile <Path_to_CA_Certificate.ca-bundle> | |
<FilesMatch "\.(cgi|shtml|phtml|php)$"> | |
SSLOptions +StdEnvVars | |
</FilesMatch> | |
<Directory /usr/lib/cgi-bin> | |
SSLOptions +StdEnvVars | |
</Directory> | |
BrowserMatch "MSIE [2-6]" \ | |
nokeepalive ssl-unclean-shutdown \ | |
downgrade-1.0 force-response-1.0 | |
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown | |
</VirtualHost> | |
</IfModule> |
References:
[1] https://wiki.apache.org/httpd/RewriteHTTPToHTTPS
[2] https://wiki.apache.org/httpd/RedirectSSL