Scheme-relative URLs

I really love the IT industry so much, I’ve been hacking away in this industry for nearly 20 years and I am still learning new things. Today’s little gem is scheme-relative URLs (aka protocol-relative URLs).

This solves the age-old problem of creating URLs for other domains which work when you’re in SSL mode, I mean who hasn’t written code to determine whether you’re in SSL mode and so using either “http” or “https” for creating off-site URLs? In fact, in Rails there’s even request.scheme built into the framework. Well, no more I say!

Believe it or not, hidden away in RFC1808 (ok, not really hidden at all) is the fact that a relative URL can be something like this: //example.com/ in which case it’s relative to the current scheme – so if the document containing that URL is requested over HTTPS then that URL will be https://example.com/ and if over HTTP then the URL will be http://example.com/

And yes, before you ask, it works in CSS, link tags, script tags, hrefs and even IE6!! – it’s been around since 1995, we’re all just dumb-asses!

kthxbai

Posterous theme by Cory Watilo