Absolute vs relative URLs

后端 未结 12 1630
时光取名叫无心
时光取名叫无心 2020-11-21 05:31

I would like to know the differences between these two types of URLs: relative URLs (for pictures, CSS files, JS files, etc.) and absolute URLs.

In addition, which o

12条回答
  •  轮回少年
    2020-11-21 06:05

    See this: http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax

    foo://username:password@example.com:8042/over/there/index.dtb;type=animal?name=ferret#nose
    \ /   \________________/\_________/ \__/            \___/ \_/ \_________/ \_________/ \__/
     |           |               |       |                |    |       |           |       |
     |       userinfo         hostname  port              |    |       parameter query  fragment
     |    \_______________________________/ \_____________|____|____________/
    scheme                |                               | |  |
     |                authority                           |path|
     |                                                    |    |
     |            path                       interpretable as filename
     |   ___________|____________                              |
    / \ /                        \                             |
    urn:example:animal:ferret:nose               interpretable as extension
    

    An absolute URL includes the parts before the "path" part - in other words, it includes the scheme (the http in http://foo/bar/baz) and the hostname (the foo in http://foo/bar/baz) (and optionally port, userinfo and port).

    Relative URLs start with a path.

    Absolute URLs are, well, absolute: the location of the resource can be resolved looking only at the URL itself. A relative URL is in a sense incomplete: to resolve it, you need the scheme and hostname, and these are typically taken from the current context. For example, in a web page at

    http://myhost/mypath/myresource1.html
    

    you could put a link like so

    click me
    

    In the href attribute of the link, a relative URLs used, and if it is clicked, it has to be resolved in order to follow it. In this case, the current context is

    http://myhost/mypath/myresource1.html
    

    so the schema, hostname, and leading path of these are taken and prepended to pages/page1, yielding

    http://myhost/mypath/pages/page1
    

    If the link would have been:

    click me
    

    (note the / appearing at the start of the URL) then it would have been resolved as

    http://myhost/pages/page1
    

    because the leading / indicates the root of the host.

    In a webapplication, I would advise to use relative URLs for all resources that belong to your app. That way, if you change the location of the pages, everything will continue to work. Any external resources (could be pages completely outside your application, but also static content that you deliver through a content delivery network) should always be pointed to using absolute URLs: if you don't there simply is no way to locate them, because they reside on a different server.

提交回复
热议问题