如约而至

php一句代码实现http跳转https

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »

ThinkPHP用户直接插入到index.php,其他框架插到入口某共公加载的文件中,即可一句代码实现整站跳转https。本站为实例。

//方法一 https状态
if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off'){
Header("HTTP/1.1 301 Moved Permanently");
header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}

//方法二 判断端口
if($_SERVER['SERVER_PORT']=="80"){
Header("HTTP/1.1 301 Moved Permanently");
header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}

内容来源于网络。