简体   繁体   中英

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.

 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 .

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. to avoid duplicating it

Or even this as you only need to switch the beginning of the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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