0

There are a lot of questions on SO regarding scheme relative URL, but I don't understand what will happen in these scenarios:

1) I am on HTTPS clicking on href="//example.com/" (example.com doesn't have SSL (it's HTTP), so browser will try to open HTTPS://example.com/ (because it wants to match the current scheme) and if there won't be HTTPS scheme it will open HTTP://example.com/?

2) Vice-versa going from HTTP to HTTPS, when the target //example.com/ is only HTTPS. Will browser open HTTPS if the destination target does not have HTTP?

unor
  • 92,415
  • 26
  • 211
  • 360
Edgar
  • 898
  • 2
  • 12
  • 36

2 Answers2

2

The browser will try to open the URL using the same scheme it's currently on; if it's currently on HTTPS, it will request the URL with HTTPS and vice versa for HTTP. If the target server does not support that scheme, it will simply fail. In case of a server which only supports HTTPS, that usually means that it enforces HTTPS; if you make an HTTP query to that server it often simply redirects to the HTTPS version of the same page. That's entirely up to the server to do though.

If a server only supports HTTP, that usually means that it doesn't have HTTPS at all. In that case an HTTPS request would simply fail and the browser will display an error message along the lines of "couldn't establish a secure connection/couldn't connect to server".

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thank you! Now it's clear to me that I shouldn't just change all URL's in my project to relative scheme URL's... Before I thought that `//` has got some logic in it... and won't fail. Maybe you could give me an example when do you use (if you use it at all) relative scheme url in your projects? – Edgar Feb 08 '16 at 11:03
  • 2
    It's a good idea for external resources, like `//cdn.jquery.com/latest.js`, or something along those lines. As long as you're sure that external resource offers both HTTP and HTTPS versions, this is a good thing to do. Arguably you could then simply *always* use the HTTPS version, but... oh well... (hand waving)... avoid HTTPS overhead unless necessary... mumble mumble... – deceze Feb 08 '16 at 11:10
0

I have found the way how to do this with some inspiration from the answer on how to link to different port as I needed to do both. The way is:

<a href="/vnc.html" onclick='javascript:event.target.port=6080;event.target.protocol="https:"'>VNC connection</a><br />
Roman Pavelka
  • 3,736
  • 2
  • 11
  • 28