7

Please help me understand the functionality of the <base> tag's href attribute.

I have a website hosted at http://goodsite.org/bob. other sites are hosted at http://goodsite.org/others.

I am planning to move my application to http://bettersite.org/bob.

In order to support this I have a base url in my header. <base href="/bob/">.

My links look something like this: <a href="thatonething.img">That one thing</a>

As hoped I am seeing the links as expected (e.g. "http://goodsite.org/bob/thatonething.img")

Question:

Does <base> tag universally support root relative urls (i.e. href="/bob/")?

This seems to work in my testing but I am having a little more trouble reading through the specifications. More generally I'm wanting to know if I am misusing this tag.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Ynot
  • 561
  • 4
  • 10
  • 1
    Possible duplicate of https://stackoverflow.com/questions/1889076/what-are-the-recommendations-for-html-base-tag?rq=1 – Asons Jul 31 '18 at 19:07
  • @LGSon, not a duplicate. I posted after reading that question because it is lacking information (even in all the answers) relating to the use of root relative (path-absolute) urls – Ynot Jul 31 '18 at 19:58
  • Well, if you search for _root_ you'll find a few comments, still, this one is clarify that, and is a possible duplicate: https://stackoverflow.com/questions/4764405/how-to-use-relative-paths-without-including-the-context-root-name?noredirect=1&lq=1 – Asons Jul 31 '18 at 20:56
  • 1
    @LGSon, thank you very much for looking at this. The link you provided does touch on this in a couple answers. Having already read that answer I created this question. I do know that this can work as evidenced by my question. What I'm really hoping to do is build consensus that this is actually a proper way to use base tag (i.e. is to spec). I did try to decipher the specifications around tag but it proved to difficult for me to decipher. – Ynot Aug 03 '18 at 17:14

1 Answers1

12

Does <base> tag universally support root relative urls (i.e. href="/bob/")?

Yes, <base> accepts domain-relative (path-absolute) URLs and will correctly resolve them relative to the host name.

<base href="/bob/"> means that you want all path-relative URLs (i.e. URLs that contain just a path but don't start with /) to be relative to /bob/, such that

  • <a href="foo.html"> will point to /bob/foo.html, regardless of the domain.

Domain-absolute and path-absolute links will not be adjusted: respectively,

  • <a href="https://stackoverflow.com"> will still point to https://stackoverflow.com, and
  • <a href="/bar"> will still point to /bar.

As long as this is how you intend for your path-relative links to behave, you're not misusing the <base> element this way. As always, when using the <base> element it's a good idea to test your links from time to time to make sure they're still pointing to the right URLs.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356