Home > web developing > Base-tag-href

Base-tag-href

March 2nd, 2008

 I was pretty confused about the <base /> tag in HTML. If your base tag look like this:

<base href="http://somedomain.com" />

and your link looks like this:

<a href="page1.html">page1</a>

your link would point to http://somedomain.com/page1.html

If your base tag looked like:

 <base href="http://somedomain.com/folder" /> *whithout a trailing slash*

your link would still point to http://somedomain.com/page1.html

If you add the slash -> <base href="http://somedomain.com/folder/" />

your link would now point to http://somedomain.com/folder/page1.html

Now, as I can see it. If you add a "/" in front of your link 

<a href="/page1.html">page1</a>

and your base still looks like

<base href="http://somedomain.com/folder/" />

The link would point to http://somedomain.com/page1.html ignoring the /folder/

 This page http://www.drostdesigns.com/base-href-tag/ tells people always to add a "/" in front of all the link, but then you are not able to put subdirectories in your base tag.

This page http://www.cs.tut.fi/~jkorpela/HTML3.2/5.7.html makes an example without the "/" in the link.

It makes more sense to me, not to add the "/" in front of all the links, making it easy to change the base href to whatever you want, but maybe I have missed something.

 

web developing ,

  1. March 4th, 2008 at 11:12 | #1

    You seem a bit confused…
    The notation is the same as for Unix directory system: the / in front of a path tells to start from the root directory, which is the top-level domain.
    The primary interest of the base tag is to tell where is the root of the site: it allows to use a unique directory for images, one for CSS, one for JS, and so on, put just below the root, even if your page is located deep in a hierarchy.
    Alternatives:
    - Don’t specify a base. Files without an initial slash are reached relative to the location of the page, which might result in ../../images/foo.gif with number of .. depending on the depth. Can be fine for page specific files, annoying for files (images, CSS, JS) common to the whole site.
    - Give a base with a folder, like you show. Files are relative to this folder, which can be interesting if you have several distinct parts in your site (the blog, the forum, etc.).

  2. Johan Laidlaw
    March 4th, 2008 at 11:39 | #2

    You’re right.. I’m (was) confused :) The reason I wrote this was because I started working on a site where the base was set to the root (www.test.somedomain.com) and I was going to work local on my mac (http://localhost/~username/domain). All the links were set with the / in front, meaning I could not place the site where I wanted it to be. Now we have removed all the starting slashes in the links and the base tag I guess will be needless.

  1. No trackbacks yet.