JavaScript :始终重定向到 HTTPS

JavaScript : Always Redirect To HTTPS

本文关键字:HTTPS 重定向 JavaScript      更新时间:2023-09-26

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

这段代码是否正确?

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

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

试试这个

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