Nginx http to https redirection in php

by Francis 2014-09-12 14:39:34

Nginx http to https redirection in php
When we use in nginx httpt to https redirection, (the apache https does not configured) use $_SERVER['HTTP_X_FORWARDED_PROTO'] checks and redirect the headers.
 
<?php
if (!isset($_SERVER['HTTP_X_FORWARDED_PROTO']))
{
header("HTTP/1.1 301 Moved Permanently"); // Http link move to 301 permanently

if(substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.')
{
$pageRedirect = "https://www.".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
else
{
$pageRedirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
header("Location: $pageRedirect");
exit(0);
}
elseif(substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.')
{
header("HTTP/1.1 301 Moved Permanently"); // Http link move to 301 permanently
$pageRedirect = "https://www.".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location: $pageRedirect");
exit(0);
}
?> 


 
1090
like
0
dislike
0
mail
flag

You must LOGIN to add comments