Make your own tiny URL service in Drupal with Shorten or ShURLy modules

Short (or tiny) URL's are great for sharing links on various social media services. Twitter's 140 character limit places a premium on bytes better used for words than URL's, e-mail clients get hinky with long URL's, and so on. It's possible to use services like tinyurl.com or bit.ly to create your short links, but as Drupal users we have a powerful platform at our fingertips. Maybe it can do the short URL natively or maybe it can be programmed to automatically retrieve short URL's from the existing services.

Fortunately everything is possible given a choice of a few Drupal modules. What I most want to share is the steps to build your own tiny URL service using Drupal.

Let's consider some goals:

  • Custom tiny URL's so you have control over your personal branding rather than supporting the branding of bit.ly
  • Own your tiny URL infrastructure rather than remaining at the mercy of 3rd parties (ask yourself.. what country corresponds to the .ly top-level domain? what country is having a little civil war currently? what country might possibly cut off its Internet like a neighboring country did a couple months ago? Do you want to risk your tiny URL's to potentially volatile countries?)
  • Automatically generate tiny URL's for each node
  • Automatically insert the generated tiny URL into tweets or other social media messages generated by the website
  • Lightweight and easy to use

Before we get to the real purpose of this, I have a couple small issues to cover first.

The simplest method: no modules at all

The simplest method is to simply not use the pathauto module or otherwise generate nice pretty URL's for your nodes. example.com/node/12345 is pretty close to being a tiny URL, isn't it? If your site doesn't use pathauto to generate pretty URL's the default is node/12345. You could declare yourself "done" right now.

Except ... pathauto is really cool for SEO purposes because there's a lot of SEO value for descriptive URL's.

In theory if you install the Global Redirect module you could advertise your node/12345 URL as the short URL, and Global Redirect will take care of redirecting from node/12345 to the clean descriptive URL.

Your tiny URL infrastructure

By this phrase, "your tiny URL infrastructure", I mean the tiny URL's you advertise into the Internet that map to URL's on your site. If you carefully always use tiny URL's to advertise pages on your site, for the public to reach your site they must traverse through your published tiny URL's to get to your site. Suppose the tiny URL service you used were to die? Suddenly the people trying to use your advertised tiny URL's would be unable to get to your site. Major FAIL

It's not unknown for popular web service sites to die and leave users in the lurch.

Integration with social media tools

It's a major pain when you need a short tiny miniscule URL to have to manually copy your long beautiful descriptive SEO friendly URL into a service, generate the URL, then paste it into a tweet or other social media gizmo. Right?

It's much better if it's done automatically.

There are plenty of Drupal modules which neatly integrate social media services into your website. I like the Sexy Bookmarks module, and there is modules for Twitter, Add This, Share This, and more. It'd be way convenient for users of the social media buttons on your website to automatically get a short URL for their tweet, rather than the long one.

It'd also be way cool for the short URL to be conveniently presented to the user so they can use it however they wish. They may even think you're even more uber-cool than ever, eh?

The Shorten module, and friends

The Shorten module is an excellent wrapper around several tiny URL services: http://drupal.org/project/shorten

It's easy to install. It's primarily an API module and there are plenty of modules who use Shorten to get short URL's. It does have a couple user interface elements, block's and pages your users can use to create shortened URL's as well as an administrator area for configuring the module. Everything is well thought out and easy to use.

Out of the box it supports calls to 25 different tiny URL services.

The shorten module does almost everything I mentioned above. It's easy to use, light weight, integrates with social media, easy to install, no muss, no fuss.

The only issue I have with it is that it relies on 3rd party services. Maybe you're unconcerned over the risk of a 3rd party link service dying, and I completely understand that. But it's also pretty easy to take one of two further step and have control over your tiny URL infrastructure.

The ShortURL module

The first module we'll discuss is ShortURL: http://drupal.org/project/shorturl

ShortURL integrates with Shorten as a service and generates short URL's completely within your Drupal site. Hence you wouldn't be dependent on 3rd party URL shorteners.

It's trivial to install and automatically becomes available within Shorten as one of the services your users can use.

The ShURLy module

The last module we'll discuss, ShURLy, is an even more awesome step forward: http://drupal.org/project/shurly

On the one hand it performs the same sort of service as ShortURL in that it installs alongside Shorten and appears as a service within Shorten.

It goes quite a bit further than ShortURL however by providing several useful widgets and reports:

  • Click-to-copy-to-clipboard functionality on short URLs
  • Click tracking on each short URL
  • “Drag to bookmark bar” Javascript bookmarklet to create URLs quickly from any web page
  • Magic happens during hook_boot() for fast, efficient, and early redirection.
  • Web services API to shorten/expand URLs

But, wait, there's more! Did you see "Web services API"?

Use ShURLy to build a link shortening service of your own

ShURLy let's you take a step beyond shortening your URL to operating your own service. Obviously this works if you have the wherewithall and desire to set up another domain and Drupal instance. And the setup of this is not the easiest. But it's pretty darn cool, and it's something you could operate for several websites or as a public service. I just don't see it being something to make you a zillionaire.

There's two (or more) drupal instances to consider: a) the link shortener site, b) the client sites

On the client sites simply install the Shorten module but don't yet configure any services. We'll get back to them in a minute.

On the link shortener site install: ShURLy, Views and optionally Shorten

Out of the box ShURLy gives a non-compelling non-friendly user interface. If you want to operate a service for the public it will be necessary to spend time on user interface design to make it usable. If you want to see what I mean head over to u.7gen.com (my shortener) and poke around.

Enable the "ShURLy Service" module. Then read shurly_service/README.txt for instructions.

As a stripped down service-only drupal instance you probably want to turn off most of the permissions and most of the features. Some permissions to configure are:

  • manage Shorten URLs API keys: Allows to create their own API key
  • Create short URLs: Allows to create short URL's

In shurly_service/README.txt you'll see several service URL's which can be used to programmatically create URL's.

Such as: http://example.com/shurly/api/shorten?longUrl=http://www.example2.com

A GET operation on that URL does the shortening, return it in the response body. Adding a format= option controls the format of the response body.

Give "anonymous" the "Create short URLs" permission and anybody can create shortened URL's. FYI It appears that for the service to work anonymous must have this permission. I wasn't able to get the service to work until giving "anonymous" the "Create short URLs" permission. This strikes me as a potential magnet for spammers.

Go back to your client site(s). In the Shorten admin area (admin/settings/shorten) there is a tab labeled Custom Services. You'll find a "Title", an "API Endpoint URL" and some checkboxes.

For API Endpoint URL enter: http://example.com/shurly/api/shorten?longUrl=

Shorten will append the long URL to the URL you give. Because the default format is JSON, click the JSON button.

You could stop at this point. Under the hood your two Drupal sites will talk to each other generating shortened URL's. It's really cool.

Let's take another step and associate shortened URL's with specific users. One reason is if you have multiple client sites, you may want the shortened URL's for each client site to be separated from the other client sites.

Using the API key lets you do this.

On the link shortener site, make the block named "Short URL API key" show up somewhere ... This block lets the user generate an API key for themselves. (FYI I don't understand why this is a block - it should be a tab on the users My Account page) The API key is of course a long hex string.

When a registered user has generated their API key the API Endpoint URL becomes: http://example.com/shurly/api/shorten?apiKey=___HEX__STRING___&longUrl=

Summary

I hope you've enjoyed learning about this and made it all the way to the bottom of the tutorial. it's a cool service and especially as I own an already short domain (7gen.com) I'd always wanted to have my own link shortener on my own domain rather than use 3rd party service. But it wasn't a strong enough desire to go ahead and do it myself.

What do you think? Let me know in the comments boxes below.