Redirecting from HTTP to HTTPS with PHP in Nginx, Apache & IIS

I use the php function to redirect a website. It’s easy, I just need to call the function on the page in the right place where I request a redirect from http to https.


<?php

 

function r_redirecttohttps()

{
if (!(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' ||
$_SERVER['HTTPS'] == 1) ||
isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))


{
$redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $redirect);
return(0);
}

}

?>

I try something like this, should work for php on Nginx, Apache and IIS

Leave a Reply

Your email address will not be published. Required fields are marked *