且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Blogger中使用相关链接

更新时间:2023-02-27 09:28:13

Blogger has <$BlogURL$> tag

<$BlogURL$> points to the URL of your blog.

Usage: A good value for a "home" link, or the "back to current" link in an archive template, e.g.

<a href="<$BlogURL$>">home</a>

Can also be used as a base href, to make relative links work in both the main page and in archive pages in a separate directory.

So you can do

<a href="<$BlogURL$>/12/2013/how_to_do.html">Your URL</a>


Blogger makes relative links (links like <a href="archives/archive.html"> are relative, links like <a href="http://www.domain.com/archives/archive.html"> are absolute links) a bit more difficult to manage than usual, because most people keep their archives in a different directory than their main page. If you link to "images/mypic.jpg" in a post, then when that post is archived the link will point to "archives/images/mypic.jpg" and thus not be found. The same thing happens if you put a relative link in your main template, because the archive pages are also created from your main template. Fear not, there are two simple fixes.

Fix one is to use a "base href" tag to tell the browser to start every relative url with the path to your main directory. In your main template, in the <head> section, include the tag <base href="<$BlogURL$>">. If you have the separate archive index page style of archives, include a base href in the section of your archive template as well. The next time you publish or post & publish, all your relative links will start from the directory where you have your main page, whether they are in that directory, or in your archives directory. Just be sure you remember to change the Blog URL in your settings if you move your blog!

The second fix is to use server-rooted paths rather than relative paths: a server rooted path starts with a slash, and lists all the directories from the root directory of the server. For example, if your main page was www.example.com/index.html and your archives were in www.example.com/archives/archive.html, then a server-rooted link to the archives would be <a href="/archives/archive.html">, and a server-rooted link to an image in www.example.com/images/ would look like <img src="/images/image.jpg">. However, if your main page is in www.freehost.com/bunch12/~username/blog/blogger.html, a server-rooted link will start /bunch12/~username/blog/, and using a base href tag will be a whole lot easier (once you get used to it).

Source