【问题标题】:How to switch from http to https client side [closed]如何从http切换到https客户端[关闭]
【发布时间】:2016-11-12 11:05:18
【问题描述】:

如何在客户端脚本中从 http 切换到 https。请勿更改网址否则。它不应该在 https 中执行任何操作。服务器对 http 和 https 的回复相同。我需要原始 URL 的其余部分保持不变。

http://example.com --> https://example.com
http://example.com/a --> https://example.com/a
http://example.com/b --> https://example.com/b

【问题讨论】:

  • 我不明白你的意思。 HTTPS 用于与服务器进行安全通信。如果不将 URL 更改为 https:// 并向服务器发出新请求,就无法在客户端切换到 HTTPS。这就是练习的重点
  • 简单地获取当前位置并将http替换为https怎么样?
  • 谁来做那个客户端?重点是什么?只需从服务器返回一个重定向。
  • 无法更改服务器

标签: javascript browser https


【解决方案1】:

您可以使用 javascript window.location.href 属性通过 https url 重新加载页面

// current url: http://example.
if(window.location.href.substr(0,5) !== 'https'){
  window.location.href = window.location.href.replace('http', 'https');
} // new url: https://example.

【讨论】:

  • 如何保持 URL 的其余部分相同?
  • 我编辑了我的答案
  • 已经在 https 上会发生什么?
  • @WalleCyril if 语句确保仅在协议不是 https 时才执行重定向。
猜你喜欢
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 1970-01-01
  • 2019-05-23
  • 2010-10-13
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
相关资源
最近更新 更多