您当前的位置:易学堂 > 运维教程

PHP实现http与https转化

时间:2019-01-22 17:04:50

前言

在配置完SSL证书,要实现全站https,需要实现HTTP转化为https,将htttp全部跳转为https,由于使用的PHP,这里用PHP代码实现转化,当然用服务器脚本实现更好(你没权限的话就算了)。

http转化为https  

<?php  
//http转化为https   
if ($_SERVER["HTTPS"]<>"on")  
{  
$xredir="https://".$_SERVER["SERVER_NAME"].  
$_SERVER["REQUEST_URI"];  
header("Location: ".$xredir);  
}   
?>

https转化为http

<?php  
//https转化为http   
if ($_SERVER["HTTPS"]=="on")  
{  
$xredir="http://".$_SERVER["SERVER_NAME"].  
$_SERVER["REQUEST_URI"];  
header("Location: ".$xredir);  
}   
?>

结语

一般将以上代码放在网页开头即可,即可实现http和https的转化。