【问题标题】:Check web page url and make https if entered as http检查网页网址,如果输入为 http,则为 https
【发布时间】:2013-09-17 14:13:40
【问题描述】:

我有一个使用 https 路由的网页,并且一切正常。 我注意到我可以将其更改为 http 并且页面仍然加载。

我正在尝试检查窗口 url,如果输入为 http,则将其设为 https。仅适用于此页面并使用 Jquery 或 Javascript。 (我知道大多数人会建议不要使用安全脚本)

这对我不起作用:

<script>
var url = window.location.pathname;
if url.match('^http://')
{
url = url.replace(/^http:\/\//, 'https://');
    window.location.pathname = url;
}

谢谢

【问题讨论】:

  • 你应该在服务器端做这个。
  • 这可能对您不起作用,因为您在 if 的条件周围缺少括号。我很确定这至少是其中的一部分。但是,是的,在服务器端进行。
  • 应该使用 .htaccess 来完成。见stackoverflow.com/questions/13977851/…
  • window.location.pathname 返回 URL 中 .tld 之后的路径。您想使用window.location.protcol。但即便如此,这种类型的操作最好在 .htaccess 中完成。
  • CSRF 或 XSS 能否将其作为脚本并使其工作?

标签: javascript html


【解决方案1】:

如果你真的想做客户端:

<script>

   if (  window.location.protocol != 'https:' ) {
           window.location = document.URL.replace("http://","https://");
    }

</script>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-01-08
  • 2012-01-25
  • 2014-11-19
  • 2015-06-22
  • 2016-04-10
  • 2012-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多