Relative URLs for HTTP and HTTPs

Ajaxian pointed to a post by Ned Batchelder on relative urls. I almost didn’t click through to the post because I didn’t think there was anything new to learn about URL syntax. Boy was I wrong.

Have you ever seen a url that looks like this?

  • <img src=’//fast.cdn.net/pix/smiley.jpg’ />

We’ve also created links that started from the first slash and dropped the domain, but I’ve never seen links that dropped the http or the https from the link. Ned explains the benefits of this technique thusly:

Here, we’ve left off the protocol scheme, but included a host name. In this case, the protocol scheme from the displayed page will be used, but against the host in the URL. The relative URL system is still in play here: omitted portions of the URL at the beginning are taken from the base page, and the relative URL takes over whereever it starts. On an HTTPS page, this will be an HTTPS request to the CDN, on an HTTP page, it will be an HTTP request.

I love it when I learn something new about a piece of technology I’ve taken for granted for years.

3 thoughts on “Relative URLs for HTTP and HTTPs”

  1. I’d like to do the opposite…

    -force the protocol
    -but use the current page domain

    something like:

    link

    thanks!

Comments are closed.