Redirecting from HTTP to HTTPS

February 8, 2015 6k views


I have an SSL certificate from a vendor installed and I want all visitors who come via HTTP to be redirected to HTTPS website-wide, but no matter what I try, I cannot figure out how to do it here. After more than a day trying many suggestions from numerous forums and Digital Ocean’s tutorials, I still cannot get it and I am running out of options. I tried:

a) The permanent redirect in the port 80 Host configuration file( “Redirect permanent /" That always results in a redirect loop.

b) I have tried implementing numerous suggestions from multiple forums and blogs as to rewriting rules in Again, I either get the loop, or no redirecting to HTTPS.

c) I tried editing the .httaccess file. No redirecting to HTTPS.

Can anyone provide a substantive guide as to how to trouble shoot this?

My configuration is underneath the line below.THANKS!


I have two virtual hosts, one for port 80 ( etc/apache2/sites-available/ and another for port 443( /sites-available/mysite-sll.conf).

The port 80 host’s configuration is as follows:

DocumentRoot /var/www/mysite/public
<Directory /var/www/mysite/public>

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUESTFILENAME} !-d
RewriteCond %{REQUEST
RewriteRule ^ index.php [L]

The port 443 host's configuration is as follows:

<IfModule mod_ssl.c>
<VirtualHost _default_:443>

DocumentRoot /var/www/mysite/public
SSLEngine on
SSLCertificateFile /etc/ssl/private/mysite_com.crt
SSLCertificateKeyFile /etc/ssl/private/
SSLCertificateChainFile /etc/ssl/private/client.crt

3 Answers

In your port 80 virtualhost I would recommend replacing:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUESTFILENAME} !-d
RewriteCond %{REQUESTFILENAME} !-f
RewriteRule ^ index.php [L]


RewriteEngine on

# force ssl
RewriteCond     %{SERVER_PORT} ^80$
RewriteRule     ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

In order for this to work you will need to ensure that you have the rewrite module active in apache:

a2enmod rewrite

and once you have made the changes you will want to restart apache for them to take effect:

service apache2 restart
  • i got it working however it still says connection is insecure in browser address bar and an exclamation mark over in firefox, in safari it shows nothing, instead of HTTPS icon in address bar its showing nothing

Thanks a lot, ryanpq. I found the problem, which was not related to the Virtual Hosts settings. The culprit was one of my SSL settings at CloudFlair, which kept redirecting users who tried to access port 443 back to port 80, hence the loop.

@ryanpq - Worked like a charm! So easy but for a novice this is a wealth of value! Thank you!!!

Have another answer? Share your knowledge.