js 实现 http 强制重定向至 https

js 实现 http 强制重定向至 https

 

var reg = /^(192.168.1.)/;
    if(window.location.hostname !== 'localhost' && !reg.test(window.location.hostname)) {
      // 判断非本地server时 http强制转换成https
      var targetProtocol = "https:";
      if (window.location.protocol != targetProtocol)
      window.location.href = targetProtocol +
      window.location.href.substring(window.location.protocol.length);
    }

 

posted on 2020-08-21 10:23  梦幻飞雪  阅读(3870)  评论(1编辑  收藏  举报