php、JS代码 http强制跳转https

admin2023-03-3022917

html代码 http强制跳转https


代码如下:


<script type="text/javascript">

   var targetProtocol = "https:";

   if (window.location.protocol != targetProtocol)

   window.location.href = targetProtocol +

   window.location.href.substring(window.location.protocol.length);

</script>



  1. //方法一 https状态

  2. if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off'){

  3.     Header("HTTP/1.1 301 Moved Permanently");

  4.     header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

  5. }

  6. //方法二 判断端口

  7. if($_SERVER['SERVER_PORT']=="80"){

  8.     Header("HTTP/1.1 301 Moved Permanently");

  9.     header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

  10. }



网友评论