Creating a quick and dirty short URL in ExpressionEngine

0 comments

Short URLs are a fun way to share your blogs and news around on Twitter etc without using up too many of those precious 140 characters. Twitter has it’s own shortening serving, t.co that does this very well. Perhaps an act of vanity, but having your own short URL format is not an overly complicated thing to set up at it’s most basic level.

A little while ago, we bought the moogal.me domainname and set to forward onto moogaloo.com at a DNS level. this means anyone going to http://moogal.me will be redirected on to moogaloo.com.

We then wanted a way to create a short link that can be used on Twitter to direct people back to our blogs.

Using the full URL, eg http://moogaloo.com/blog/return-of-the-flexibly-priced-project/ would use up way to much of a tweet to be practical. Typically this might be shortened down, depending on your Twitter client / service to something like: moogaloo.com/blog/return-of… or http://bit.ly/zTwm2N

What we’re aiming for is moogal.me/b/568

As mentioned earlier, the moogal.me simply forwards on to moogaloo.com. The /b is the template group that contains the page that handles the redirect. All that’s in there is:

{exp:channel:entries channel=”blog” disable=”pagination|trackbacks” limit=”1” status=”open” dynamic=”yes” cache=”yes” refresh=”60” entry_id=”{segment_2}”} {redirect=’blog/{url_title}’} {/exp:channel:entries}

This looks in the blog channel, takes the URL structure and finds the specific entry by the entry_id as found in the second segment, 568 in the above URL example. A bit of caching helps performance, dynamic keeps the channel:entries tag looking in the URL for the right blog, and we only need the one entry, so no need for sorts and orders.

Once we know the blog we’re looking for, it’s just a case of redirecting the user straight onto the actual blog, which is what the {redirect=’blog/{url_title}’} does.

We could also put in a require_entry=”yes” parameter in there with a {if no_results} {redirect=”404”} {/if} so any URL hacking or bad URLs get redirected onto a 404 page.

But that’s all there is to it. More technical folk may have more fancy ways of doing proper 301 redirects at an .htaccess level, but that’s not me!

0 comments