http访问跳转至https
网站安装SSL证书后,未https访问根据端口判断,并自动转至https
if($_SERVER['SERVER_PORT']=='80'){
header("Location: https://www.abc.com".$_SERVER['REQUEST_URI']);
exit;
}
增加对域名的判断
switch(strtolower($_SERVER['SERVER_NAME'])){
case 'abc.com':
if($_SERVER['SERVER_PORT']=='80'){
header("Location: https://www.abc.com".$_SERVER['REQUEST_URI']);
exit;
}
break;
}对是否为https方式访问的判断,不能单纯靠 $_SERVER['SERVER_PORT']=='80
建议方法:
function is_https() {
if(isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT']==='443'){
return true;
}elseif( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'){
return true;
}elseif( !empty($_SERVER['HTTP_FROM_HTTPS']) && strtolower($_SERVER['HTTP_FROM_HTTPS']) !== 'off'){ //西部数码
return true;
}else{
return false;
}
}
使用.htaccess重定向
RewriteEngine On
RewriteCond %{HTTP:From-Https} !^on$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?abc.com$ [NC]
RewriteRule ^(.*)$ https://www.abc.com/$1 [R=301,L]
