简体   繁体   English

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

[英]Redirection to Secured Server (https)

I don't have access to the .htaccess file, so I am using below JavaScript for redirection from http to https and append www. 我无权访问.htaccess文件,因此我在JavaScript下面使用从http重定向到https并附加www。

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

The problem is that when a user enters http://www.example.com , it redirects to https://www.www.example.com . 问题在于,当用户输入http://www.example.com时 ,它将重定向到https://www.www.example.com

Please help me resolving the issue. 请帮助我解决问题。

Try this: 尝试这个:

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

Simply remove the www. 只需删除www. to avoid duplicating it 避免重复

Or even this as you only need to switch the beginning of the url: 甚至是这样,因为您只需要切换url的开头:

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

You should set your server to accept www.example.com OR just example.com , then it doesn't matter whether the user has added www or not 您应该将服务器设置为接受www.example.com或仅接受example.com ,那么用户是否添加了www无关紧要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM