2

How to switch from http to https in a client side script. Do not change the URL otherwise. It should do nothing in https. The server replies the same to http and https. I need the rest of the Original URL to stay intact.

http://example.com --> https://example.com
http://example.com/a --> https://example.com/a
http://example.com/b --> https://example.com/b
Walle Cyril
  • 2,691
  • 2
  • 16
  • 46
  • 3
    I do not understand what you mean. HTTPS is for communicating securely with the server. There's no way to switch to HTTPS on the client side without changing the URL to a https:// one and making a new request to the server. That's the whole point of the exercise – Pekka Nov 12 '16 at 11:07
  • 1
    What about simply taking the _current_ location and replacing `http` by `https`? – arkascha Nov 12 '16 at 11:18
  • 1
    Who do that _client-side_? What's the point? Just return a redirect from the server. – VLAZ Nov 12 '16 at 11:21
  • Cannot change the server – Walle Cyril Nov 12 '16 at 11:29
  • 1
    Possible duplicate of [Detect HTTP or HTTPS then force HTTPS in JavaScript](http://stackoverflow.com/questions/4723213/detect-http-or-https-then-force-https-in-javascript) – Gökhan Kurt Nov 12 '16 at 13:42

1 Answers1

1

you can use javascript window.location.href property to reload page with https url

// current url: http://example.
if(window.location.href.substr(0,5) !== 'https'){
  window.location.href = window.location.href.replace('http', 'https');
} // new url: https://example.
erwan
  • 823
  • 8
  • 17