346

有什么方法可以检测 HTTP 或 HTTPS,然后强制将 HTTPS 与 JavaScript 一起使用?

我有一些用于检测 HTTP 或 HTTPS 的代码,但我不能强制它使用https:.

我正在使用window.location.protocol属性来设置站点的任何内容,https:然后刷新页面以希望重新加载加载到浏览器中的新 https'ed URL。

if (window.location.protocol != "https:") {
   window.location.protocol = "https:";
   window.location.reload();
}
4

13 回答 13

568

试试这个

if (location.protocol !== 'https:') {
    location.replace(`https:${location.href.substring(location.protocol.length)}`);
}

location.href = blah将此重定向添加到浏览器历史记录。如果用户点击后退按钮,他们将被重定向回同一页面。最好使用location.replace它,因为它不会将此重定向添加到浏览器历史记录中。

于 2011-01-18T11:02:54.773 回答
68

设置 location.protocol 导航到一个新的 URL。无需解析/切片任何东西。

if (location.protocol !== "https:") {
  location.protocol = "https:";
}

Firefox 49 有一个可以正常工作但不能正常工作的错误。据说在 Firefox 54 中已修复httpshttps:

于 2012-04-05T21:08:21.600 回答
25

这不是一个好主意,因为您只是临时将用户重定向到 https,而浏览器不会保存此重定向。

您描述了网络服务器(apache、nginx 等)http 301、http 302的任务

于 2012-07-07T12:20:09.513 回答
16

这个怎么样?

if (window.location.protocol !== 'https:') {
    window.location = 'https://' + window.location.hostname + window.location.pathname + window.location.hash;
}

不过,理想情况下,您会在服务器端执行此操作。

于 2011-01-18T11:03:42.283 回答
16
if (location.protocol == 'http:')
  location.href = location.href.replace(/^http:/, 'https:')
于 2014-01-10T00:07:32.497 回答
8

你应该检查这个: https ://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/upgrade-insecure-requests

将此标记添加到您的 index.html 内

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

希望它有所帮助。

于 2020-03-04T09:42:41.320 回答
5

不是 Javascript 方法来回答这个问题,但如果您使用 CloudFlare,您可以编写页面规则,将用户更快地重定向到 HTTPS,并且它是免费的。在 CloudFlare 的页面规则中看起来像这样:

在此处输入图像描述

于 2016-01-02T07:10:08.970 回答
3

你可以做:

  <script type="text/javascript">        
        if (window.location.protocol != "https:") {
           window.location.protocol = "https";
        }
    </script>
于 2019-07-24T00:45:51.270 回答
3

我喜欢这个问题的答案。但是为了有创意,我想再分享一种方式:

<script>if (document.URL.substring(0,5) == "http:") window.location.replace('https:' + document.URL.substring(5));</script>
于 2019-12-20T03:29:56.700 回答
2

下面的代码假定变量“str”包含您的 http://.... 字符串。它检查它是否是 https,如果 true 什么都不做。但是,如果它是 http,它会将 http 替换为 https。

if (str.indexOf('https') === -1) {
  str = str.replace('http', 'https')
}

于 2020-06-20T23:15:31.817 回答
1

功能方式

window.location.protocol === 'http:' && (location.href = location.href.replace(/^http:/, 'https:'));
于 2019-06-27T13:47:28.483 回答
-2

嗨,我使用此解决方案效果很好。无需检查,只需使用 https。

<script language="javascript" type="text/javascript">
document.location="https:" + window.location.href.substring(window.location.protocol.length, window.location.href.length);
</script>
于 2016-08-06T12:35:30.200 回答
-2

我刚刚通过Pui Cdm测试了所有脚本变体,包括上面的答案和许多其他使用 php、htaccess、服务器配置和 Javascript 的答案,结果是脚本

<script type="text/javascript">        
function showProtocall() {
        if (window.location.protocol != "https") {
            window.location = "https://" + window.location.href.substring(window.location.protocol.length, window.location.href.length);
            window.location.reload();
        }
    }
    showProtocall();
</script> 

vivek-srivastava提供的 效果最好,您可以在 java 脚本中添加进一步的安全性。

于 2017-06-19T12:03:26.083 回答