怎么设置http跳转到https?在为网站部署SSL证书后,可以实现https加密访问,但是网站的用户往往习惯了http访问,这个时候我们就需要设置访问http时自动跳转到https
ecshop程序级跳转方法(多变php服务中心就是采用这种方案实现的
打开includes/init.php
查找
/* 取得当前ecshop所在的根目录 */
define('ROOT_PATH', str_replace('includes/init.php', '', str_replace('\\', '/', __FILE__)));
给其上方添加
if(!((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on')||(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])&&$_SERVER['HTTP_X_FORWARDED_PROTO']=='https')))
{
Header("HTTP/1.1 301 Moved Permanently");
header('Location: https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
}
php常用解决方法
if ($_SERVER["HTTPS"] <> "on")
{
$xredir="https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; header("Location: ".$xredir);
}
JS常用解决方法
<script type="text/javascript">
var url = window.location.href;
if (url.indexOf("https") < 0)
{
url = url.replace("http:", "https:");
window.location.replace(url);
}
</script>
本文地址:https://www.phpicu.com/article.php?id=40