重定向到安全服务器(https)

Redirection to Secured Server (https)

本文关键字:https 服务器 安全服 安全 重定向      更新时间:2023-09-26

我没有访问.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.

请帮我解决这个问题。

试试这个:

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也无所谓