I'd debug why... it looks like it should work.
Add this somewhere on the page when viewing the https version:
echo("https val: ".$_SERVER['HTTPS']);
What is the result?
Based on the results of these two tests I should be able to give you code that would work I think.