Search code examples
phpforwarding

PHP https url forwarding


Ok so I need my site to automatticly forward all requests to the server for http to https. I was using php with no real problems but now godaddy is telling me that I need to change the way i am doing this. Is my code correct, should i change how im doing this, what method is better? Thanks in advance for the help!

<?php
if ($_SERVER['SERVER_PORT']!=443)
{
$url = "https://". $_SERVER['SERVER_NAME'] . ":443".$_SERVER['REQUEST_URI'];
header("Location: $url");
}
?>

Solution

  • You don't need to include the port number... https' default port is 443 already. You'd only need an explicit port number if you're running an https service on some OTHER port. As well, why the duplicate redirect? You've got 2 if() blocks doing the exact same thing.

    However, just testing for port 443 is not sufficient. You can run ANY service you want on any port. It's just that HTTPS usually is on 443. But nothing says it HAS to be. There's $_SERVER['HTTPS'] which explicity tells if you if HTTPS is in use or not.