Question

Redirecting from HTTP to HTTPS

  • Posted February 8, 2015

PROBLEM:

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(mysite.com.conf): “Redirect permanent / https://mysite.com/." That always results in a redirect loop.

b) I have tried implementing numerous suggestions from multiple forums and blogs as to rewriting rules in mysite.com.conf. 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!

LAMP

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

The port 80 host’s configuration is as follows:

ServerName mysite.com ServerAlias www.mysite.com DocumentRoot /var/www/mysite/public <Directory /var/www/mysite/public>

Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </Directory>

The port 443 host’s configuration is as follows:

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

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

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

In your port 80 virtualhost I would recommend replacing:

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

with:

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

In your port 80 virtualhost I would recommend replacing:

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

with:

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

@ryanpq , thanks it worked

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

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.