Question

Redirecting from HTTP to HTTPS

  • Posted on February 8, 2015
  • SherlasmAsked by Sherlasm

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


Submit an answer

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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!!!