JS强制重定向网页地址,http转为https

1,881 阅读1分钟

在index.html文件里面的head的script里面加入这句话。

var targetProtocol = "https:"
        if (window.location.protocol != targetProtocol) {
            window.location.href =targetProtocol + window.location.href.substring(window.location.protocol.length);
        }  

!!!注意本地的测试也要变为https ,不然无法访问,解决方法是开启本地的https,或者可以做个判断,判断环境为dev环境就不定向,为prod环境再定向