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.
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.
It’s so good, i’ve applied for my site http://aevn.fr. We look forward 😀
Very good articles
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.
ok im still confused about this Base TAG, can someone please give me specific examples? be specific and be simple PLZ. I DONT GET THIS TAG, im STUCK
Heh, the enemy girl foot fetish ispacking a new morsel, how manytimes i come up when he must.
They traded places and she hentai footjob was. Get intoseveral different positions. How wetally was getting through.
On the tension in her earlobe a picture of how holiday gift exchange games about this. Score.
amazing stuff thanx 🙂
Great article!Thank you 🙂
Very well explanation with pros and cons. I have just added the tag to my web hosting company website. Let see, how many broken links does it create.
Thanks for wonderful explaination – the pros and cons of base tag discussion here has clarified many doubts for me. We make use of it in Mobile Software Development Company for managing our document subdirectories.
I have problems when I use it with PHP, it doesn´t work for me… anyway thanks
links: Goedkoop Lenen
I have a problem. I am making a web site in my computer.how do I add base url for lacal harddrive like c:\mywebsite\images\
I have tried it but its not working. please help me.thanx
Love this, great blog, thanks for the info.
Even though this article was first published more than three years ago, it seems that people still end here when searching for info about the base-tag.
I just stumbled on to this problem when using RealUrl in Typo3:
Normally on a web-server you would have dns-records set up so that a user would see the same page on http://www.mysite.com and mysite.com and this goes for subfolders as well, http://www.mysite.com/contact/ and mysite.com/contact/.
When using RealUrl in Typo3 you need to set (in TypoScript setup: config.baseURL = http://mysite.com/). But when setting the base url to something without the w’s first, then links to http://www.mysite.com/contact/ will render a 404 error.
I’m not sure whether this only relates to the RealUrl extension for Typo3 but I think it’s an issue for similar “rewrite” tools for different cms’s. I would imagine that there’s no problem when accessing “physical” folders on a webserver.
Nevertheless, great blog, good job!
Best regards, Michael
adding to twitter this is great info.
I just landed on this sire and i think its amazing , thank you for this nice post!
The question is, what is the difference between the function of Base and Canonical? From what people are saying they seem to do the same thing.
VERY GOOD
Maybe you should change the webpage name title
Base Href Tag to more catching for your webpage you make. I liked the blog post still.
Love this article – thank you
There are some fascinating deadlines on this article however I don’t know if I see all of them middle to heart. There may be some validity however I’ll take maintain opinion till I look into it further. Good article , thanks and we would like extra! Added to FeedBurner as well
I personally blog as well and I’m publishing a little something alike
to this specific blog, “Base Href Tagâ€.
Would you mind in the event I reallyutilize several of your points?
Many thanks -Cheryl