News Archive
PhpRiot Newsletter
Your Email Address:

More information

Short URLs

Note: This article was originally published at Planet PHP on 22 February 2011.
Planet PHP

Short URLs

London, UK Tuesday, February 22nd 2011, 09:43 GMT

When using twitter, or other mobile devices where space is a premium, it is quite common to use shortened URLs. Many people will not be keen on typing, and will prefer something like instead 1.

The URLs for my articles are automatically generated from the title. There is a little function that uses translit's transliterate() function to ASCII-ify the titles. It transforms for example "pfcongrez, P2P DsDDNDuNDuDNDN, php|tek, PHP Vikinger, DPC and eZ Conference and Awards" to "pfcongrez-p2p-konferenciya-phptek-php-vikinger-dpc-and-ez-conference-and-awards" with code similar to:

$titleShort = transliterate($fullTitle, array('cyrillic_transliterate', 'lowercase_latin', 'normalize_ligature', 'diacritical_remove', 'normalize_punctuation', 'remove_punctuation', 'spaces_to_underscore', 'compact_underscores'), 'utf8', 'us-ascii');

However, is hardly a short URL. What we want is something short and descriptive. First of all, I thought it would work to generate short URLs automatically, following the following algorithm:

  1. Strip out all chars that are not A-Z, a-z, 0-9, ., space and -.

  2. Split the title up in words, splitting on space.

  3. If the sanitized URL is shorter than 14 chars: join the words back together with a "-", otherwise:

  4. Loop over each word, replacing each word with the first letter of the word, unless it is one of the important words (such as "php" or "xdebug").

  5. Add a marker for the date, to ensure that short URLs are unique. The date marker is generated like:

    1. Format the date as two digit year, ISO week and ISO weekday

    2. Convert the created number to base 36 with:

    php -r "echo date_create('20090410')-format('yWN');" 09155 php -r 'echo base_convert("09155", 10, 36), "\n";' 72b

Our long title from above, then turns into pp-p-php-vdaecaa-72b. Although relatively short, it is still not a descriptive URL. To be honest, it's a bit difficult to come up with a short, descriptive URL for this title, so let us examine another one, of a more recent post.

The title "64-bit integers in MongoDB" is converted into "6iim-7yp", which is again short, but not very informative; and "Xdebug 2.1 Released" is converted into "xdebug-2.1-7x2", which does make sense.

Because I did not get very good results with automatically converting titles into sort URLs, I decided that for all new articles I will just define my own.

Now, once we have the short URLs, they need to be used. Instead of my long main domain, I registered a new one just for short URLs: to reduce the URL length even more. For the current article, the length of the URL shrinks from 39 to 26 characters. The short URL is also embedded in the html source with a rev=canonical tag and a rel=shortlink tag, such as:

This tag is meant to provide alternative URLs for the current one. For more information on rev=canonical see or Chris Shiflett's entry, and for rel=shortlink see the "/

Truncated by Planet PHP, read more at the original (another 1024 bytes)