Simple Short URLs with ExpressionEngine
Posted in
If you use Twitter, you’ve no doubt seen short URLs. Posting most web page URLs would use up almost all of the 140 characters allowed by Twitter. Services like http://bit.ly and http://tr.im shorten any URL to just a few characters, leaving plenty of room for your comments. But history has shown these third party services may not be reliable. That’s why it might be best to roll your own…and ExpressionEngine can make it easy.
ExpressionEngine creates very descriptive, keyword-rich URLs. And while the URLs are friendly, they can get quite long (this page’s URL is 77 characters long). But every ExpressionEngine entry also has a ‘short URL’ in the form of its ID number. And when used in a URL, the ‘URL title’ is interchangeable with the ID.
So http://www.sundog.net/sunblog/posts/simple-short-urls-with-expressionengine/
can become http://www.sundog.net/sunblog/posts/3487/ with very little effort.
We can get the URL shorter still, but we’ll need to create a new template group. The shortest a template group name can get is a single letter, and I chose ‘e’ (for ‘entry’). PHP parsing needs to be enabled for the index page, with parsing order set to ‘Output’.
The code in the template is very simple:
{exp:weblog:entries}
<?php
header("Location: {url_title_path=sunblog/posts}", true, 301);
exit;
?>
{/exp:weblog:entries}
The EE tags are parsed first (because PHP parsing order is set to ‘output’), which dynamically (based on the entry id) fills in the location for the PHP redirect. The url_title_path is the full text URL.
Using this method we have reduced this:
(77 characters) - http://www.sundog.net/sunblog/posts/simple-short-urls-with-expressionengine/
down to this:
(25 characters) - http://sundog.net/e/3487
Keeping the domain name in the link adds authenticity and credibility. That’s important because third party services can be used to distribute malware. And because the entry id in ExpressionEngine is the permanent identifier for all entries, the short URL will still work even after changing the url title of the entry.
I have added the short URL to the bottom of our entry template, so it is now available for all our blog posts. Feel free to use them for all of your link posting needs.
Don't miss any posts! Subscribe to our blog feed or only posts by Erik Uetz.
Short URL: http://sundoginteractive.com/e/3487


Comments
Be the first to comment!
Leave A Comment