Problem with Site Running Both HTTP & HTTPs
What web server are you running? I ask because IIS will define the $_SERVER['https'] variable even if the site is being accessed via HTTP (it's value will be 'off') and Ben's answer is only checking if the $_SERVER['https'] variable is present.

One solution is to add a further check that $_SERVER['https'] == 'on'

Try updating this:

// Ensure our urls have the right scheme
define('URI_SCHEME',  ( isset($_SERVER['HTTPS'] ) ) ? "https://" : "http://" );


to this:

// Ensure our urls have the right scheme
define('URI_SCHEME',  ( isset($_SERVER['HTTPS'] ) &amp;&amp; $_SERVER['HTTPS'] == "on" ) ? "https://" : "http://" );