Protocol relative urls and mixed content »

Protocol relative urls and mixed content.

The best solution when you need http and https on the same page.

Have you ever had the need of mixed content on the same page? I mean content coming from http and https sources? Usually you get some advice from browser, but you can avoid this using the protocol relavite urls.

What does this mean? This

<img src="//carloamoroso.it/image.jpg" />

is a perfect valid URL! As a matter of fact, I didn't knew this feature of the protocol till i was faced to solve a problem of mixed content coming from http and https on the same page.

Going to the page where you find the full specs (rfc) for URL  you can notice this definition:

<scheme>://<net_loc>/<path>;<params>?<query>#<fragment>

So that's why it works and it makes perfect sense! The browser get the "protocol" from its address bar and all URLs will be relative to THAT protocol. So <path> can be relative to <net_loc> or.. to <scheme> providing the <net_loc>.

So next time you need to mix content coming from different protocol source (from the same <net_loc>, obviously) you can use this technique of  protocol relative urls to avoid browsers warnings. :-)

28 Mar 09

Commenta