You can change:
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
to:
if (array_key_exists('HTTPS', $_SERVER) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
array_key_exists returns a boolean. Since I check it first and use && the if statement will exit before checking the value of $_SERVER["HTTPS"] if it doesn't exist.