Are colons allowed in URLs?

16,768

Solution 1

Colons are allowed in the URI path. But you need to be careful when writing relative URI paths with a colon since it is not allowed when used like this:

<a href="tag:sample">

In this case tag would be interpreted as the URI’s scheme. Instead you need to write it like this:

<a href="./tag:sample">

Solution 2

Also note the difference between Apache on Linux and Windows. Apache on Windows somehow doesn't allow colons to be used in the first part of the URL. Linux has no problem with this, however.

Solution 3

Are colons allowed in URLs?

Yes, unless it's in the first path segment of a relative-path reference

So for example you can have a URL like this:

And you can use it normally as an absolute URL or some relative variants:

<a href="https://en.wikipedia.org/wiki/Template:Welcome">Welcome Template</a>
<a href="/wiki/Template:Welcome">Welcome Template</a>
<a href="wiki/Template:Welcome">Welcome Template</a>

But this would be invalid:

<a href="Template:Welcome">Welcome Template</a>

because the "Template" here would be mistaken for the protocol scheme. You would have to use:

<a href="./Template:Welcome">Welcome Template</a>

to use a relative link from a page on the same level in the hierarchy.

The spec

See the RFC 3986, Section 3.3:

The path component contains data, usually organized in hierarchical form, that, along with data in the non-hierarchical query component (Section 3.4), serves to identify a resource within the scope of the URI's scheme and naming authority (if any). The path is terminated by the first question mark ("?") or number sign ("#") character, or by the end of the URI.

If a URI contains an authority component, then the path component must either be empty or begin with a slash ("/") character. If a URI does not contain an authority component, then the path cannot begin with two slash characters ("//"). In addition, a URI reference (Section 4.1) may be a relative-path reference, in which case the first path segment cannot contain a colon (":") character. The ABNF requires five separate rules to disambiguate these cases, only one of which will match the path substring within a given URI reference. We use the generic term "path component" to describe the URI substring matched by the parser to one of these rules. [emphasis added]

Example URL that uses a colon:

Share:
16,768
Emanuil Rusev
Author by

Emanuil Rusev

Designer, developer, minimalist, maker of Nota, Historie, Parsedown.

Updated on June 11, 2022

Comments

  • Emanuil Rusev
    Emanuil Rusev almost 2 years

    I thought using colons in URIs was "illegal". Then I saw that vimeo.com is using URIs like http://www.vimeo.com/tag:sample.

    1. What do you feel about the usage of colons in URIs?
    2. How do I make my Apache server work with the "colon" syntax because now it's throwing the "Access forbidden!" error when there is a colon in the first segment of the URI?