Create Short URLs on the fly in your drupal website

| | 1 min read

SEO requires you to have descriptive titles and URLs matching the title. But when you want to publish your URL some place where the length of the URL is an issue you definitely want short URLs. One solution to the problem is to use URL shortening services like tr.im to shorten the long URLs to short and sweet 9 character urls. But the URL will lose your sites identity. Don't worry there is the drupal way to solve this problem.

There is a URL shortening module that you can install in Drupal to do just this for you. The module, actually a pair of modules, Shorten and Short URL will allow you to run your own sweet URL shortening service on your website.

But what if you really do not want one more module on your site just for a few short URLs. Drupal has one more trick up it's sleeve. You can use the trusted drupal_goto in a node and get just the same effect. Create a new story and set the url alias as the shortened alias that you want. Make sure you uncheck 'Automatic alias' so that pathauto will not come into play here. Set the input format to 'PHP code' and save

<?php
if(!variable_get('cron_semaphore', FALSE)) {
drupal_goto('the_long_drupal_url_you_want_shortened', NULL, NULL, 301);
}
?>

as the body of the story. You are all set and your shortened URL is ready to rock :-).