What are the recommendations for html <base> tag?

前端 未结 17 2271
一向
一向 2020-11-21 10:17

I\'ve never seen HTML tag actually used anywhere before. Are there pitfalls to its use that means I should avoid it?

The fact that I have never noticed

17条回答
  •  青春惊慌失措
    2020-11-21 10:44

    To decide whether it should be used or not, you should be aware of what it does and whether it's needed. This is already partly outlined in this answer, which I also contributed to. But to make it easier to understand and follow, a second explanation here. First we need to understand:

    How are links processed by the browser without being used?

    For some examples, let's assume we have these URLs:

    A) http://www.example.com/index.html
    B) http://www.example.com/
    C) http://www.example.com/page.html
    D) http://www.example.com/subdir/page.html

    A+B both result in the very same file (index.html) be sent to the browser, C of course sends page.html, and D sends /subdir/page.html.

    Let's further assume, both pages contain a set of links:

    1) fully qualified absolute links (http://www...)
    2) local absolute links (/some/dir/page.html)
    3) relative links including file names (dir/page.html), and
    4) relative links with "segments" only (#anchor, ?foo=bar).

    The browser receives the page, and renders the HTML. If it finds some URL, it needs to know where to point it to. That's always clear for Link 1), which is taken as-is. All others depend on the URL of the rendered page:

    URL     | Link | Result
    --------+------+--------------------------
    A,B,C,D |    2 | http://www.example.com/some/dir/page.html
    A,B,C   |    3 | http://www.example.com/dir/page.html
    D       |    3 | http://www.example.com/subdir/dir/page.html
    A       |    4 | http://www.example.com/index.html#anchor
    B       |    4 | http://www.example.com/#anchor
    C       |    4 | http://www.example.com/page.html#anchor
    D       |    4 | http://www.example.com/subdir/page.html#anchor
    

    Now what changes with being used?

    is supposed to replace the URL as it appears to the browser. So it renders all links as if the user had called up the URL specified in . Which explains some of the confusion in several of the other answers:

    • again, nothing changes for "fully qualified absolute links" ("type 1")
    • for local absolute links, the targeted server might change (if the one specified in differs from the one being called initially from the user)
    • relative URLs become critical here, so you've got to take special care how you set :
      • better avoid setting it to a directory. Doing so, links of "type 3" might continue to work, but it most certainly breaks those of "type 4" (except for "case B")
      • set it to the fully qualified file name produces, in most cases, the desired results.

    An example explains it best

    Say you want to "prettify" some URL using mod_rewrite:

    • real file: /some/dir/file.php?lang=en
    • real URL: http://www.example.com/some/dir/file.php?lang=en
    • user-friendly URL: http://www.example.com/en/file

    Let's assume mod_rewrite is used to transparently rewrite the user-friendly URL to the real one (no external re-direct, so the "user-friendly" one stays in the browsers address bar, while the real-one is loaded). What to do now?

    • no specified: breaks all relative links (as they would be based on http://www.example.com/en/file now)
    • : Better already. But relative links of "type 4" are still broken (except for "case B").
    • 0 讨论(0)
提交回复
热议问题