JavaScript:始终重定向到HTTPS

时间:2016-07-06 04:10:11

标签: javascript https web

所以我有这个网站,我希望它总是在任何人在网上浏览时重定向到HTTPS。我想使用JavaScript

这段代码是否正确?

if(window.location.protocol != "https:") {
    var currentURL = window.location.href;
    window.location.replace("https" + currentURL.substring(4));
}

我还是个菜鸟。请帮帮我:)。

1 个答案:

答案 0 :(得分:-1)

试试这个

if (window.location.protocol == "http:") {
    var mainurl = window.location.href.substr(5);
    window.location = "https:" + mainurl;
}
相关问题