强制跳转 https 的几种方法

花生 可爱的博主

时间: 2021-07-08 阅读: 89 字数:1262

{}
开发过小程序案例的猿友一定知道的,今天就不讲https证书的申请,配置。回头会详细的讲一下https证书的申请以及配置,这次简单的分享一下如何使用.htaccess配置http强制跳转https

强制跳转 https 的几种方法

html 文件,head 中加入如下meta
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
javascript 脚本,适用于IIS服务器的 403-4.htm 或者 403.htm
<script type="text/javascript">
var url = window.location.href;
if (url.indexOf("https") < 0) {
    url = url.replace("http:", "https:");
    window.location.replace(url);
}
</script>
php 脚本
if ($_SERVER["HTTPS"] <> "on"){
    $xredir="https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    header("Location: ".$xredir);
}
.htaccess文件,适用于Apache 配置文件的Directory标签内
RewriteEngine on
RewriteBase /yourfolder
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
Nginx服务器,在 server { ... } 中插入
    rewrite ^(.*)$  https://$host$1 permanent;
本文章网址:https://www.sjxi.cn/detil/abb1ff45441842f59a79edbe79dae582

最新评论

当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号