This is the celebrated journal of Mr. Simon Collison A.K.A Colly

EE Shortener plugin: Your own short URLs

12th April 2009

I’m now providing my own short URLs for all posts on this here blog (you’ll see one just above the title), essentially divorcing myself from all those third-party link shortening services. This post briefly describes the problem, and provides an EE plugin to enable your own short links with ease.

The problem

As an avid Twitter user I’m all too aware of the spread of confusing shortened URLs - the TinyURL, and stuff. Over the last week, many have written about the problems of such URLs - bad usability, privacy loss, and the reliance on third-party URL-shortening mediators that could disappear at any time. Certainly, its unlikely that third-party short URLs will be around as long as most of the content they link to.

Thus, its up to us to provide our own short URLs for our own content, and we need to do three things:

  • provide a meta link element with the rev=“canonical” and rel=“alternate shorter” attributes in the post’s head.
  • Provide a visible, clickable a link in the post’s body, again with rev=“canonical” and rel=“alternate shorter” as attributes.
  • Create a permanent redirect to the actual post, at its natural, elongated URL.

At present, only a few big sites (the likes of Flickr, Dopplr, are doing this, but as more sites embrace it, and more bloggers do the same, we’ll eventually have the critical mass, and maybe when we post those big links into twitter it’ll automatically find the better short URLs we provide. Its worth the effort, however you look at it. There is already a tool that sniffs for your own short links, and even a short link sniffing bookmarklet.

Bringing this to ExpressionEngine

EE is ready to make this relatively easy using the {exp:weblog:entries} tags. Most of us use {url_title} to provide more human-readable URLs of course, but we can use the post’s {entry_id} - a natural numeric reference to a post - as part of a shorter URL. Combine this with a redirect template and we’re almost there.

EE plugin: Shortener

To make implementation easier, and hopefully encourage many in the EE community to embrace the provision of short URLs, I’ve pulled together a quick plugin.

You need to add two plugin tags to your main post (single entry) template, and create a new template group through which the permanent redirects will happen. Here’s the basic set-up:

Firstly, create a new template group with a really short name, such as “post” or “short” or even “s” - anything you want. The redirects will be parsed via this template, so it is essential.

For that group’s index template, you must turn on PHP to output.

Into that template, paste the following tag (make sure the tag is within PHP opening and closing tags), and replace template parameter with the path to your usual single entry page, and segment parameter should specify location of url_title:

{exp:shortener:redirect template="blog/entry" entry_id="{segment_3}"}

Next, in the head of your single-entry template (the template used to show each post), add the following tag. Substitute the template parameter with the name of your new short template group, and segment parameter should specify location of url_title:

{exp:shortener:meta template="s" url_title="{segment_3}"}

This will create the following bit of meta in the head:

<link rev="canonical" rel="alternate shorter" href="" />

Finally, create a visible, clickable shortlink for the visitor, wherever you want in the body of your single-entry template, by adding the following tag. Substitute the template parameter with the name of your new short template group, and segment parameter should specify location of url_title:

<p>Shortlink: {exp:shortener:link template="s" url_title="{segment_3}"}</p>

This will create a clickable link (with the rev=“canonical” rel=“alternate shorter” attributes) visible to your visitors:

Short link:

As you’ll see, I’ve placed this within a paragraph and added a label, but you can do whatever you want to suit your design.

Note: If you have several weblogs and therefore several single-entry templates, you’ll need to repeat the process and amend template paths for each.

The result

Well, a typical blog post around these parts looks something like this:

Thanks to my shortened URLs, I can target this using:

In that example, “s” is my new template group, and “765” is the entry_id of the post itself. This will automatically redirect to the full, readable URL.

Download and caveats

Edit: Version 1.4 now removes the “www” from short URLs, and supports any path (including use of index.php or a renamed file). The PHP is also more efficient, and it fixes a couple of bugs that were in the previous version.

Download pi.shortener.php (version 1.4)

The rev attribute has not been included in the HTML5 specification, although its realistic to believe that if the masses continue to make use of it, it’ll end up back in the spec. Thus, we also include the rel=“alternate shorter” attribute.

Note also that I can’t shorten your domain names! This only works within the site’s domain. I know that some people with long domain names are acquiring shorter domains to use as the host for their short URLs. With a bit of work this plugin could be tweaked to support external short domain redirects, though i doubt it could redirect to the url_title URL.

The plugin automatically sniffs out the schema (http://) and host (your domain) from the URL of your site. If this causes problems for anyone do let me know, as it might be better to pass the whole path using the parameter.

You need to be using the url_title in your links. If you already use an entry_id (I guess that’s very, very few of you) in your links, this won’t work.

To keep your URLs as short as possible, it is worth using htaccess to get rid of the unnecessary /index.php/ stuff (as many of us do) that EE puts in your URLs by default. See the Knowledge Blog article. The plugin will work with index.php or a renamed version - just include it with the template variable.

To conclude

For those of you who don’t use EE, I should explain that the reason we do the redirect with PHP via plugin and template is because we want the user to end up at a URL using the full readable url_title. Its not possible to query the database to convert entry_id into url_title using a htaccess file, hence doing it this way instead.

I’ve tested the plugin on my own blog, which has one weblog, and it works a treat. Hopefully it’ll work well for you. If not, drop me a comment. If you can improve my PHP, then please do - and let us know on here!

Further reading and tools

There are posts-galore about short URLs and rev=“canonical” at the moment. Here are a few I’ve found useful:


# Matthew Pennell responded on 12th April 2009 with...

Nice work, Simon. I would have also included the rel=“alternate shorter” link element in the head as well.

The only drawback with this is that it doesn’t work for those of us with unwieldy long domain names that want to redirect to a shorter version (like Simon W has).

I guess the simplest solution would be to set up the alternate domain to 301 redirect to the same URL on the main domain, and then use your extension to handle the request once it hits the main site. That could be done with nothing but .htaccess, I would imagine.

# Matthew Pennell responded on 12th April 2009 with...

PS: Stick your code on GitHub with everyone else! ;)

# Simon Collison responded on 13th April 2009 with...

Hi Matthew,

I’m still unsure (as it seems a lot of folks are) about whether to go with rev=“canonical” or rel=“alternate shorter”. I’m gonna see how it all pans out over the next few weeks, but I guess it comes down to how brave we might all be about using a non-HTML5 attribute. I might amend teh plugin to offer users a choice. For now, they could edit the plugin to change the outputted HTML if they prefer rel=“alternate shorter” or variant I suppose.

I agree about my method only working within the host domain (as mentioned in the article). Its cool for me because my domain is, but not so good if your domain is etc. Never gonna get a short URL with something like that unless you host links at or something.

As you say, my method could work if I allow users to specify a domain in the template path, so the link points to a separate domain, but it starts getting away from an EE-only solution, as the user would have to set up the redirect on a separate domain, and I’m not prepared to detail such an involved solution.

My main motivation though is to not use a htaccess file as I really believe its important that we can use the database during the redirect process to take the user to a readable URL using the url_title.

There will be solutions I expect. Its cool all this because we’re all only just starting to think it through. In a month or so I expect there’ll be a more flexible EE plugin from someone else, and also (hopefully) a more agreed approach to which elements and attributes we use. Still some disagreements going on out there.

# Malarkey responded on 13th April 2009 with...

I implemented this in less than a minute on FABW. @ioubeer mate!

# Luke Dorny responded on 13th April 2009 with...

This is awesome.
Thank you so much for writing about it.

I recently tweeted that all websites should create a shortened url feature so that the world can begin tweeting without the need for shortyurl and all the others.
Even better would be a stats plugin for tracking usage within your EE system. Even better would be stripping out the WWW as mentioned on though i’ve heard to the contrary, shortened urls is one of the massive fringe benefits of doing this.
Even better would be a built-in url shortener alongside your Write Post page that you can quickly use to shorten external links while writing a post to your site.

The only site that i’ve seen use short urls to great advantage is product details pages.

Thanks, Simon!

# Simon Collison responded on 13th April 2009 with...

Malarkey: My pleasure. Thank you for using the plugin.

Luke: You’re right about the benefits of stripping out the www. Its my inability at exploding URL strings with PHP at fault there. I can still remove it when I use my short URL though, because my hosting is set up to work with or without www. For example, will still redirect to this page.

You’ve got me thinking about stats tracking for the short URLs now. Because we use the entry_id I think its perfectly possible with a view_count variable (inherent in EE). My brain hurts right now but I’ll come back to it.

Finally, yep, also agree about having a built-in URL shortener in the Publish page. EE does of course allow you to define your own url_titles, and to a certain extent my short URLs require a set structure (template/id), but yeah, to cloak that with a short word, so we end up with something like would be fab. Again, I need a rest, but I’ll come back to it and try these added bonuses.

# Bridget Stewart responded on 13th April 2009 with...

I implemented this just so that I could try out how it works. It’s really not going to help me out at the moment given that the subdomain.domain string I have right now is long. However, the plugin was pretty easy to use. It took me a few attempts to get the stuff inside the php to work right (my own fault, of course), but all in all it wasn’t difficult.

Thanks for doing this!

# Simon Collison responded on 13th April 2009 with...

Luke: Version 2 now removes the “www” from the short URL. I couldn’t settle until that was fixed. Obviously requires site owners to ensure their site is available without www.

Bridget: Thanks for trying it. Fully understand that it isn’t ideal for long domain names. I reckon either myself or another in the EE community will fix that soon enough.

# Malarkey responded on 13th April 2009 with...

If you have a spare few minutes, maybe while your dinner is cooking, you could boil up something to add to “Tweet this links”:

Tweet this

# Simon Collison responded on 13th April 2009 with...

Malarkey: If I understand what you’re asking for correctly, that’s dead easy with standard EE stuff.

Once you’ve got a redirect template group sorted using the plugin, you’d just add this inside your {exp:weblog:entries} tag pair, hard-coding the shortlink path to keep it easy:

<a href=“{title}:{entry_id}”>Tweet this</a>

Or did you mean something different?

# Ian responded on 13th April 2009 with...

It’s a great plugin and one which I see becoming popular fairly quickly. As an EE user I’d be keen to make use of it on sites with short domain names.

Although not wanting to sound like a party pooper or to take anything away from the very useful plugin you’ve created the fast pace that rev=canonical seems to be moving is a little concerning as it may just help to add to the

< ahref="">

linkrot apocalyse that it is intended to prevent.

As you pointed out the rev attribute currently isn’t supported in HTML5 and could cause problems if it remains that way. I also agree with the points that Ben Ramsey made with regard to rel=“alternate shorter” or rel=“shorter” (as proposed in the WHATWG wiki) being easier to understand and less likely to make mistakes in using.

As you mentioned, it’s still a problem with longer domain names with the only workaround seeming to be to buy a shorter domain as Simon Willison has done. However this may not be a good trend either as not only will it incur more cost and maintence but could effectively mean everyone ends up with their own URL shortening services. Something which could see URLs becoming even less permanent.

Again, sorry to sound like a party pooper but just thought I’d raise a few concerns with the fact that in the space of a week it seems to have moved from problem to solution and one which isn’t an official standard. Other than that great work on the plugin. :)

# Simon Collison responded on 13th April 2009 with...

Ian: No, you’re not party-pooping at all. This stuff is ripe for discussion. I can’t disagree with anything you’ve said, and you clearly understand the good and bad of this new shift towards personal short URLs. I’m learning like everyone else.

Same as I said in response to Matthew earlier, we all need to see what happens and what the preferred methods will be. I do think that the rev attribute will end up in the HTML5 spec, but then again, I don’t know for sure. At least with blogs and dynamic CMS-based sites using templates and plugins, we can replace an attribute site-wide at any moment to comply with whatever we need to comply with that week.

By the way, I also agree about the purchase of alternate domains. Its OK for me as I have the short, but I have been thinking what to do for and some client sites. I will not buy a variant of, for example, or anything like that. I agree it seems a bit counter-intuitive or (for visitors) a bit confusing, unless perhaps if you are a massive organisation.

Ultimately, whatever happens, I still think its a good idea to provide as short a URL as possible should people want it. If, in years to come, that is simply a link without any crazy special attributes, and even if it saves just a few characters, it’ll be a positive change.

# Richard responded on 13th April 2009 with...

I have always being worried about the tinyurl etc types beginning to monetize their services and either put my content within a frameset (think that abortion of a digg bar), pop up/under before forwarding or other nasties that can present my content alongside other messages that I do not want associated with my online brand.

The attachment of links to a third party against posts and tweets etc done by me is something I have never being keen on.

As for the URL length - get another domain name - there is more acceptance of the alternate top levels for short links so a .cc or .as or similar may be an option.

# Ian responded on 13th April 2009 with...

@Colly: Absolutely. Your plugin definitely covers the reqirements for short domain names such as your own in that it provides a short link, keeps the meta information (telling the user it’s from your domain) and provides a permanent link you control. As you mentioned it’s also an easy change to rel=“alternate shorter” or whatever is eventually adopted.

For longer domains it still saves characters which all counts and I agree that any change is a positive change.

I think you’re right to keep it all EE centric with no need for htaccess modifications. This makes it easy to manage all from one place. With eventual additons as mentioned above such as integration into the Publish section of the CP and twitter integration I can see it becoming almost a default feature.

Incidentally I considered buying a shorter domain and play with htaccess redirects (301) but it seems Google will only take canonicalisation into account within a domain or across a sub-domain but not across totally separate domains.

Can this link tag be used to suggest a canonical URL on a completely different domain?

No. To migrate to a completely different domain, permanent (301) redirects are more appropriate. Google currently will take canonicalization suggestions into account across subdomains (or within a domain), but not across domains.

This was also a consideration with the other hot topic of the week - the DiggBar.

# Boyink responded on 13th April 2009 with...

Hey Colly -

I was whining for something like this a few weeks ago…so thanks for taking up the charge.

The disappointment for me is that it requires index.php to have been removed.  There are at least a few of us who choose to rename index.php rather than remove it - both for SEO keyword placement and because removal isn’t officially supported by EL (and does cause a fair number of tech support issues).

Being able to configure the plug-in to match different EE installation approaches would be killer.

# Simon Collison responded on 13th April 2009 with...

Ian: Ah, thats good info regarding a separate shorter domain. Of course, with EE’s Multiple Site Manager we could make a plugin to do the job, though as you say, Google may not view thsi favourably.

Boyink: You can still use the plugin with index.php. I misled by saying I assumed it is removed as so many do. What I meant was that its a shorter URL without it.

In the template parameter for the redirect, just specify “index.php/group/template” or whatever you’re using, and for the canonical links, specify something like “index.php/s/”.

Admittedly, using index.php makes it a longer short URL - such as All the plugin does with paths is combine domain with EE path with url_title to make complete links. Haven’t tested it with index.php though.

# Simon Collison responded on 13th April 2009 with...

Boyink: I have now tested it with index.php and it works fine. Any path will work, basically, so its very flexible. Just add the path in the parameter.

I have uploaded the plugin afresh (v1.2) with amended instructions.

# Boyink responded on 15th April 2009 with...

Thanks - will give it another go!

# Pål Degerstrøm responded on 16th April 2009 with...


My permalink structure includes year and month in the entry path. Are you OK with me modifying your source to adapt it to my sites?

Best regards,
Pål Degerstrøm

# Simon Collison responded on 16th April 2009 with...

Pål: Sure, feel free to amend a copy. You’ll find that you can already specify any path - the four parts of the URLs are put together as:

- 1. Schema (http://)
- 2. Domain (, but I remove the www)
- 3. Template via the parameter, and this can be /index.php/group/template, or /group/template, or group, or template, etc… this is the bit that you add as a parameter. I see no reason why something like blog/2009/03/30/ couldn’t be used. Its anything you like, for short template or single entry destination.
- 4. EE url_title, or entry_id for short links.

If it doesn’t work exactly as you’d like, then yep, hack it, but I will do updates as and when. Its not a particularly complex plugin to be honest.

You do have odd (but yes, readable and sensible) URLs for an EE site - reminds me of a Moveable Type structure! Don’t see many EE people doing it that way.

# Pål Degerstrøm responded on 16th April 2009 with...


Thanks, very much appreciated.

Would you care to elaborate on why you find my URLs are odd? Is it the year/month/date thing?


# Simon Collison responded on 16th April 2009 with...

Pål: Perhaps “odd” is the wrong word. I just don’t often see the /year/month/day/ format in single-entry URLs. Nothing wrong with it as far as I’m concerned, as you also have “blog” and the url_title in there for readable context. Hell, maybe its actually better for date context alongside readable context. Obviously longer though.

# Mark Perkins responded on 17th April 2009 with...

Thanks for the plugin Colly.

For those using Codeigniter to put sites together, I have created a little library that allows easy/auto creation of shortlinks.

You can find more info here -

Hope it’s useful for someone!

# jens Swelson responded on 18th April 2009 with...

Nice tool, I’m thinking about the option of implement it in my weblog.

Usefull to make it easy for other people to tweet your blogposts.

# Bobbink - 11 Internet responded on 23rd April 2009 with...

Thanks for sharing this useful information! I have implemented it now in one of my websites and I had no problems during configuration. Good luck with your websites!

# ben smithson responded on 30th April 2009 with...

Hi, I’m working (in progress) on this handy shortener and I’m stuck.

My URL is getting messed up in my browser’s title bar.  Instead of a slash (forward one, like on web directories) it’s converting to a / (ampersand pound four seven semi-colon) instead.

We use EngineHosting… I suspect it’s some super secret server setting.  Any ideas?

# Zeb responded on 3rd May 2009 with...

Found a similar plugin at Jonathan’s blog:

# Adam responded on 12th May 2009 with...

This looks great I have added this but I cant quite get it working.

I get the url created
but when I got to that page it does not change the the long URL

I am a little confused over the {segment_3} part what should I replace that with ?

my short template group is s and php parsing is set to output, should allow PHP by yes as well ?

my template for each post

# Simon Collison responded on 11th June 2009 with...

Hello everyone - thanks for the feedback, and the good number of you who have made use of this plugin.

I’ve added a new version - v1.4 - linked in the article above.

It fixes a couple of problems that we introduced (dear oh dear) in v1.3, specifically relating to paths with two segments (such as “blog/entry”) and some reported cases where slashes were turned into entities in the address bar. tested it again and all should be good now.

Adam: Yes, you must also set PHP to “Yes” in template preferences for that group.

Ben: This v1.4 will fix the problem you reported. Sorry for the probs with v1.3.

# Adam responded on 12th June 2009 with...

Updated to 1.4 and turned on PHP now working nice Thanks great plug in

# Adam responded on 12th June 2009 with...

spoke to soon all links seem to point to the same article. opps. probably me

# Adam responded on 15th June 2009 with...

Not sure but it working now, I looked at nstructions on plug in page and it advised segment_2 for the s index.php file rather than segment_3 - changing that worked for me.

Responses are now disabled Your ability to respond is disabled automatically some 30 days after articles are published, or manually much sooner if spamming guttersnipes target a particular article.

Prev 774 Next

Superfluous Aside

Archived in ExpressionEngine, Design & Web

Written in Sneinton

There are 32 responses

External References

Copyright © Mr. Simon Collison 2003-2017. Protected and licensed under a Creative Commons License. Grab the RSS feed

Engineered in Nottingham, scaffolded by ExpressionEngine, steam-pumped by United & kept alive with tea and hugs.