Relative vs. Absolute URLs
As much as I love using Movable Type, a few things have bothered me about MT, too, such as MT generating absolute URLs for internal links when relative URLs should be used. Stepan Riha’s MTRelativeURL plugin is an incredibly easy way to generate relative URLs with MT, thankfully, wherever the plugin tags are used.
For example, a permalink generated by Movable Type includes the site URL, such as this:
<a href="http://brainstormsandraves.com/ Permalink
entry_permalink/" rel="bookmark" title="Entry title">
Root relative links should instead be used for nearly all of a site’s internal sites, such as this:
<a href="/entry_permalink/" rel="bookmark" title="Entry title">Permalink
An exception where absolute URLs are required is within RSS feeds for which absolute URLs are currently still needed. So don’t use the MTRelativeURL plugin tags within your MT RSS templates, at least not unless the absolute URL requirement changes at some point.
There are at least a couple of ways to use the MTRelativeURL plugin for an entry’s permalink, but one way is to surround the
<$MTEntryLink$> tag with the MTRelativeURL tags:
<a href="<$MTEntryLink$>" rel="bookmark" title="<$MTEntryTitle remove_html="1"$>">Permalink
<a href="<MTRelativeURL><$MTEntryLink$> Permalink
</MTRelativeURL>" rel="bookmark" title="<$MTEntryTitle remove_html="1"$>">
Here’s the same example without the text wrapping: MTRelativeURL Example
See Stepan Riha’s MTRelativeURL plugin page for more information and details in its use.
Agh! Help! They’re Everywhere!
There are potentially quite a few internal links throughout your site in which MT is generating absolute URLs, not just the permalinks. This is also a very unnecessary waste of bandwidth, as I write about below in Save Bandwidth.
Over the weekend I added MTRelativeURL tags for MT-generated internal links throughout my templates. It’s an easy task that doesn’t take long while having tremendous benefit. Here’s a partial list from my own templates to give you an idea of where to look in your templates:
- Main page, individual entry pages:
- Related Categories links
- Comments / Trackbacks links
- Previous, Next links
- Main Archive page:
- Archives by Category links
- Archives by Month links
- Category Archive pages:
- Entry links
One of the benefits of using relative URLs is shaving off some webpage file size, saving some or a lot of bandwidth, depending on the page.
- I shaved off 2KB on my main page just by using the MTRelativeURL plugin for my internal links.
- Where the file size is really noticeable, though, is a high content page like my Archives by Title page that lists over 2-1/2 years' worth of entries on a single page—I shaved off 23KB just by using the MTRelativeURL plugin for all the internal links, saving bandwidth and page load time especially for dial-up users.
- I also saved 5KB with each of my Archives by Year pages that provide calendar views with links to each day’s entry, such as the 2002 Calendar Archive.
You can improve file size, save bandwidth, and have more efficient pages in other ways, too, such as getting rid of white-space within markup and content, optimizing images, using external style sheets to manage an entire site, and more. I highly recommend Andy King’s site optimization book, Speed Up Your Site.
More on Relative and Absolute URLs
A couple of helpful resources to understand relative and absolute URLs and why they matter:
- Anatomy of an http URL
Part 2 of the excellent tutorial at WebReference, It’s the Strands that Make the Web by Stephanos Piperoglou.
- Relative URLs
Part 3 of the excellent tutorial at WebReference, It’s the Strands that Make the Web by Stephanos Piperoglou.
Comments, Trackbacks: 8 so far. Add yours!
This discussion has been closed. Thanks to all who participated.