nginx
1、使用nginx的rewrite方法
配置nginx.conf
server {
listen 80;
server_name xxx.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
2、使用nginx的301状态码
server {
listen 80;
listen 443;
server_name xxx.com;
ssl on;
ssl_certificate /data/www-key/xxx.pem;
ssl_certificate_key /data/www-key/xxx.key;
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
}
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']);
}
了解 王坤的博客 的更多信息
订阅后即可通过电子邮件收到最新文章。