【问题标题】:Redirection to Secured Server (https)重定向到安全服务器 (https)
【发布时间】:2015-10-27 07:28:20
【问题描述】:

我无权访问 .htaccess 文件,所以我使用下面的 JavaScript 从 http 重定向到 https 并附加 www。

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

问题是当用户输入http://www.example.com时,它会重定向到https://www.www.example.com

请帮我解决问题。

【问题讨论】:

    标签: javascript redirect


    【解决方案1】:

    试试这个:

    if(window.location.protocol != 'https:') {
      location.href = location.href.replace("http://", "https://"); // <-- removed www.
    }
    

    只需删除 www. 即可避免重复

    甚至这样,你只需要切换 url 的开头:

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

    您应该将您的服务器设置为接受www.example.com 或只接受example.com,那么用户是否添加了www 并不重要

    【讨论】:

      猜你喜欢
      • 2011-09-18
      • 2013-12-09
      • 2019-12-25
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多