http to https redirect infinite loop problem

If you want to force ssl on your website and for some reason you can’t do it in vhost config directly, you got 3 ways to do it in htaccess.
Changes are you got a good hosting and all 3 will work for you ( for example on your own VPS ). Then there are some hostings without modssl, or with load balancers and then some of these generic rules, that work for 99% people out there will end up in endless loop.

Best to try all 3 of them, one by one and see which one works for you.

htaccess ssl redirect by %{HTTPS} variable

1st version – the most basic, works on most hostings

<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

htaccess ssl redirect by port number

2nd version – if server is using apache without modssl, then %{HTTPS} value will always be ‘off’ and therefore the rule above will cause infinite loop. Use this version instead – it checks server port instead of ^{HTTPS} variable.
It works great on websupport.sk

<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{SERVER_PORT} !^80$
	RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

htaccess https redirect infinite loop

3rd version – if all 2 above end in infinite loop, chances are your hosting is using some kind of load balancer.
Try this version then:

<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{HTTP:X-Forwarded-Proto} !https
	RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Laravel force ssl

If you are doing this on Laravel project, make sure you put this in root folder. Very often your root folder will have 4 line htaccess file which redirects everything into public folder. That’s where you need to put this. Do not put http->https rules into public folder’s htaccess, unless that is your root folder as well ( on VPS for example )

Display errors in PHP file

If you see just a blank page, chances are you have error in your PHP that you can’t see. Using these few lines you’ll be able to see the issue.

Display errors by adding code to PHP file

The easiest way to display errors in PHP file is by adding following 3 lines either at the beginning of file, or before the snippet you think is causing the issue.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Display errors by updating .htaccess file

Add following lines to .htaccess ( preferably before redirect settings, look for RewriteRule )

php_flag display_startup_errors on
php_flag display_errors on
php_value error_reporting 32767
php_flag html_errors on

32767 equals to E_ALL, see http://php.net/manual/en/errorfunc.constants.php

Also, make sure you have

display_errors = on

in your php.ini ( usually /etc/php5/apache2/php.ini, or C:/windows/php.ini )