Question

Redirecting to HTTPs from any request

Posted August 23, 2014 6.2k views

You know when you type in digitalocean.com it automatically redirects to https://www.digitalocean.com? I want that for my site! I spent a lot of time generating (and regenerating secure certs…thanks openSSL!) and I would love to force https. The only thing is I am having redirect loops and am not sure what I am doing wrong :/

Here’s my httpd.conf:
Redirect permanent / https://www.<MYDOMAINNAME>.com:443

and here’s my .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTPHOST}%{REQUESTURI}

So right now if I type in rhosfelt.com it does the automatic redirection to https, but how do I get it to do this for every subsite (/blog, /test.html etc)?

If I try to go to any other page I am getting a “This page has too many redirects” error.. Your help will be appreciated! Thanks!!!

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.

×
2 answers

This one works for m. In my .htaccess:

RewriteEngine On
RewriteCond %{SERVERPORT} 80
RewriteRule ^(.*)$ https://%{HTTP
HOST}%{REQUEST_URI} [L,R=301]

Of course, this is assuming your .htaccess is working

Yes, to get .htaccess to work you must:

set AllowOverride All in your website conf file (in /etc/apache2/sites-available/yoursite.conf) like this:

<Directory /var/www/yourwebsite>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
                Require all granted
    </Directory>

Enable mod_rewrite: sudo a2enmod rewrite
Reload apache: sudo service apache2 reload

Then your .htaccess file should work.

Submit an Answer