httpurlsslhttps

Can a URL start with "//"?


Possible Duplicate:
Is it valid to replace with // in a <script src=“…”>?

We have a site on http[s]://A.example.com/ that references some resources (JS and CSS) on http[s]://A.example.com/

It makes sense, and avoids some browser warnings, if the same scheme is used when referencing the resources as the main site A is using. Site A uses PHP, so we can programmaticly detect the current scheme (http or https) and insert that into the header of the page.

However, we have found that just using '//' instead of the scheme also works.

So site A may reference "//B.example.com/theme/main.css" without a problem in all browsers we have tried, regardless of whether we go to site A using http or https.

My question is: is this a valid URL? It seems to work, but I cannot find anyone else doing it, and I cannot find any examples of this being suggested or recommended.


Solution

  • Yes. This is known as a protocol-relative URI. Technically, it is called a "network-path reference" according to RFC 3986.

    See also Can I change all my http:// links to just //?, Why use protocol-relative URLs at all? and Network-Path Reference URI / Scheme relative URLs.