Base Href Tag
The base href tag tells the browser that all relative links within the document start from that specified base location. This allows you to view how your document will look and behave when placed on the server without actually having to do so.
Where to place the base href tag
The < base > tag goes in the < head > of the page. The < base > tag specifies the “default domain” that all domain-less links on the page are pointing to. ie
< html >
< head >
< title >Web Site Discussion< /title >
< base href="http://www.drostdesigns.com" >
head >
How the base href tag works
If you are on http://www.domain.com/page1.html then a link to /page1.html is pointing to http://www.domain.com/page1.html
If you are on http://domain.com/page2.html then a link to /page2.html is pointing to http://domain.com/page2.html
< img src=logo.gif >
In this example the full url…http://www.drostdesigns.com/logo.gif will be created due to the base href.
Without the base href this image tag would be directed to the relative location of the page.
Tips
1. Make sure that the < base > tag is placed before any links to any CSS or JS files in the < head > section
2. Make sure that your internal links to pages, files, images, stylesheets, etc all start with a / and that the URLs specify the full path to the file.
3. Make sure that you check your site using Xenu LinkSleuth for any problems.
4. The absolute correct code for a base href is < base href="http://www.domain.com/" >
5. Link to your pages like this:
/page.html
/folder/page.html
/images/widgets/blue.widget.jpg
Don’t link like this:
page.html
../../image.jpg
without a starting “/”.
If you link to an index page, then you would just use “/” or “/folder/” to link to it.
6. If you don’t want to mess with inserting the base href tag, you can add the 301 redirect to the .htaccess file in the root folder for each individual site. Here’s how:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
If you wish to read more about implementing this method read:
How to Redirect a Web Page Using a 301 Redirect
Conclusion
Insert the base href tag into the header tag for every web site you build. This will help avoid getting broken links form relative URLS.
If you want to make sure all visitors are directed to http://www.domain.com/ instead of http://www.domain.com/index.html or http://domain.com, use the 301 redirect code mentioned above.
6 Responses to “Base Href Tag”
Leave a Reply
Links
Links
August 24th, 2006 at 8:27 am
That tag is a real help when you use ‘SEO friendly URLs’, where you fool the browser into thinking it is in a subfolder. Without it you’d have to take into where the browser thinks it is when using relative links, a definite nuisance when outputting pages from a template.
A word of caution anyway: base href can unexpectedly break some stuff you don’t often have to worry about, like named anchors. If you declare a domain or a folder as your base you won’t be able to use href=#top or the like, as the browser will happily go in search of http://www.example.com#top.
Another hitch that puzzled me before I checked the server logs: paths to files in a CSS style sheet are supposed to be relative to the folder where the CSS is, but IE (who else?) manages to get it wrong for some stuff. It does indeed behave as expected with images, but it misses entirely the path with e.g. Peter Nederlof’s Whatever:hover behavior, as in (beware, invalid CSS!) someelement { behavior:url(”csshover.htc”);}.
Only a hard look at the server logs will show you why this doesn’t work with base href, with IE looking for the HTC all over the place unless you use an absolute path in your CSS or resort to some mod_rewrite magic.
October 6th, 2006 at 6:51 pm
It’s so good, i’ve applied for my site http://aevn.fr. We look forward
January 30th, 2007 at 12:57 am
Very good articles
May 20th, 2007 at 2:07 pm
According to W3.org, correct is full url, including page, i.e. http://domain.com/page.html. In this case, you can use href=#top and similar, and there is no problem with browsers or SE spiders. However, it could be problem with some link checkers, some of them are looking for /page.html/. I am using that format (with page name) on above site from over a year ago, and I didn’t see any problem.
October 2nd, 2007 at 5:19 pm
[...] Base href tag, http, https direct URLs or relative URLs on SSL pages and pop-up windows caused by state changes - http://www.drostdesigns.com/base-href-tag/ [...]
March 2nd, 2008 at 9:45 am
[...] 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 [...]