96

I just learned from a colleague that omitting the "http | https" part of a URL in a link will make that URL use whatever scheme the page it's on uses.

So for example, if my page is accessed at http://www.example.com and I have a link (notice the '//' at the front):

<a href="//www.google.com">Google</a>

That link will go to http://www.google.com.

But if I access the page at https://www.example.com with the same link, it will go to https://www.google.com

I wanted to look online for more information about this, but I'm having trouble thinking of a good search phrase. If I search for "URLs without HTTP" the pages returned are about urls with this form: "www.example.com", which is not what I'm looking for.

Would you call that a schemeless URL? A protocol-less URL?

Does this work in all browsers? I tested it in FF and IE 8 and it worked in both. Is this part of a standard, or should I test more browsers?

topher-j
  • 2,221
  • 5
  • 24
  • 35
  • 5
    possible duplicate of [Absolute URLs omitting the protocol (scheme) in order to preserve the one of the current page](http://stackoverflow.com/questions/4978235/absolute-urls-omitting-the-protocol-scheme-in-order-to-preserve-the-one-of-the) – David Feb 06 '12 at 14:40
  • 1
    For reference (since you asked what terms to search), the Google search which brought me to the original StackOverflow question was: "absolute url without protocol" – David Feb 06 '12 at 14:41
  • you're not specifying if its an http or https so the browser decides for you by taking the last page assuming that the link came from the same page. – raym0nd Feb 06 '12 at 14:41
  • Thanks David, the link to the duplicate answered all my questions – topher-j Feb 06 '12 at 14:52
  • 1
    @David: To be exactly, you wouldn’t call it an absolute URL if it doesn’t have a protocol; it’s rather a relative URL. – Gumbo Feb 06 '12 at 14:56
  • possible duplicate of [Can I change all my http:// links to just //?](http://stackoverflow.com/questions/4831741/can-i-change-all-my-http-links-to-just) – KingCrunch Feb 09 '13 at 22:56

2 Answers2

83

Protocol relative URL

You may receive unusual security warnings in some browsers.

See also, Wikipedia Protocol-relative URLs for a brief definition.

At one time, it was recommended; but going forward, it should be avoided.

See also the Stack Overflow question Why use protocol-relative URLs at all?.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Mark Maruska
  • 1,210
  • 12
  • 13
19

It is called network-path reference (the part that is missing is called scheme or protocol) defined in RFC3986 Section 4.2

4.2 Relative Reference

A relative reference takes advantage of the hierarchical syntax (Section 1.2.3) to express a URI reference relative to the name space of another hierarchical URI.

  relative-ref  = relative-part [ "?" query ] [ "#" fragment ]

  relative-part = "//" authority path-abempty
                / path-absolute
                / path-noscheme
                / path-empty

The URI referred to by a relative reference, also known as the target URI, is obtained by applying the reference resolution algorithm of Section 5.

A relative reference that begins with two slash characters is termed a network-path reference (emphasis mine); such references are rarely used. A relative reference that begins with a single slash character is termed an absolute-path reference. A relative reference that does not begin with a slash character is termed a relative-path reference.

A path segment that contains a colon character (e.g., "this:that") cannot be used as the first segment of a relative-path reference, as it would be mistaken for a scheme name. Such a segment must be preceded by a dot-segment (e.g., "./this:that") to make a relative- path reference.

Community
  • 1
  • 1
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265