php、JS代码 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>
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']);
}