Are colons allowed in URLs?

后端 未结 3 1597
Happy的楠姐
Happy的楠姐 2020-12-05 14:21

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 ab
3条回答
  •  天命终不由人
    2020-12-05 14:47

    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:

    • https://en.wikipedia.org/wiki/Template:Welcome

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

    Welcome Template
    Welcome Template
    Welcome Template
    

    But this would be invalid:

    Welcome Template
    

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

    Welcome Template
    

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

    The spec

    See the RFC 3986, Section 3.3:

    • https://tools.ietf.org/html/rfc3986#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:

    • https://en.wikipedia.org/wiki/Template:Welcome

提交回复
热议问题