Are colons allowed in URLs?

后端 未结 3 1598
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:

    <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:

    • 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
    0 讨论(0)
  • 2020-12-05 14:55

    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.

    0 讨论(0)
  • 2020-12-05 15:03

    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">
    
    0 讨论(0)
提交回复
热议问题