很多人使用了ssl后,发现httphttps都可以打开,但是不想让用户使用http协议,在服务器iisnginx 或 apache 想尽办法,各种伪静态,各种301的跳转,最终发现还是有缺陷,那么下面教大家一句php来实现

方法一: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']);
}