URL Shortener: Principles, Sample Code And Sources

URL Shortener: Principles, Sample Code And Sources
URL Shortener: Principles, Example Code And Sources

URL Shortener makes a short version of a long given URL. There are many URL shorteners out there like bit.ly, tinyurl or is.gd. Of course we can use their API to create short urls, but it’s not interesting for developers, who want to look into the very deep of the mechanism.

Short url often has structure: , where hash is a string (unique) for a long url. Hash contains only digits and letters. In some cases letters are case sensitive.

URL shortener basically has 2 jobs: generating hash (and short url) from a long url, and getting original long url from a given short url.

The second job (reverse short url to long url) is often done with mod_rewrite. Some basic rewrite rule can be like this:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?hash=$1 [L]

mod_rewrite takes the hash value from url and pass it into a variable for GET method. The script will take this hash value via $_GET['hash'] and get original long version of url from database and redirect browser’s location to it.

If our host doesn’t support mod_rewrite, then we have to pass the hash value into a variable manually, such as http://www.domain.com/?hash=abc. We can change the name for hash variable to anything to make the url shorter, for example WordPress uses 'p': http://www.domain.com/?p=100.

URL Shortener Database

Long and short versions of an url are usually stored in a database with an unique ID. So, it’s convenient to generate hash from this ID. We’ll see several ways to do that:

1. Using ID for hash

Because the ID is unique for an url in the database, so the most simple way to generate hash is using the ID itself as a hash.

This method reduces the database fields because we don’t need the hash field. A sample code for generating, storing and getting url looks like this:

function generate($longUrl) {
    global $domain; 

    mysql_query("INSERT INTO url (long_url) VALUES ('$longUrl')"); 

    return $domain . mysql_insert_id();

function getUrl($hash) {
    if (!is_int($hash)) return; 

    $result = mysql_query("SELECT long_url FROM url WHERE `id` = '$hash'");
    $result = mysql_fetch_array($result); 

    return $result['long_url'];

There are some open sources using this method:


URL Shortener - lilURL

lilURL is a simple PHP/MySQL script for generating short URLs. It’s similar to TinyURL and requires mod_rewrite. There is no API and admin panel.


URL Shortener - URLShort

urlShort is a free open source URL shortener built using PHP/MySQL. It provides API, custom names, simple and fast installation and administration, preview functionality, statistics.

2. Using base conversion

Using ID for short url is very simple, but it has a disadvantage: the url is not always short. When you have about 1000 urls in the database, the url is getting long with 4 digits: . This disadvantage comes from few characters using for hash. With integer hash, there are only 10 digits. It means there’re only 10 hashes if we use 1 digit, 2 digits – 100 hashes, 3 digits – 1000 hashes and so on.

What if we use more characters for hashes, like alphabet characters? There are 62 characters at all, including 10 digits, 52 alphabet characters (lower case and upper case). If we use all these characters, that means 1 character can represent 62 hashes, 2 characters – 622 hashes, 3 characters – 623 hashes and so on. The number of hashes is considerably increased with less characters.

This is the basic of 2nd method: use these 62 characters as a digit of a 62-base. Digits in numbers will be represented with the letters a-z, with a meaning 10, b meaning 11 and z meaning 35 and so on.

To generate a hash for an url, just convert the ID into new base. In PHP, there’s a function to do that: convert_base(). So our generating function looks like this:

function generate($longUrl) {
    global $domain; 

    mysql_query("INSERT INTO url (long_url) VALUES ('$longUrl')");
    $id = mysql_insert_id();
    $hash = base_convert($id, 10, 36);
    mysql_query("UPDATE url SET `hash`='$hash' WHERE `id`='$id'"); 

    return $domain . $hash;

function getUrl($hash) {
    if (!is_int($hash)) return; 

    $result = mysql_query("SELECT long_url FROM url WHERE `hash` = '$hash'");
    $result = mysql_fetch_array($result); 

    return $result['long_url'];

The function base_convert() can handle only lower case characters, so the maximum characters we can use is 36. It’s ok for most cases of url shortener. But if you want a really strong script that can handle all 62 characters, you can use the library Arbitrary number base conversion written by Paul Gregg.

Open sources using base conversion:

YOURLS: Your Own URL Shortener

URL Shortener - YOURLS

YOURLS is a small set of PHP scripts that will allow you to run your own URL shortening service. You can make it private or public, pick custom keyword URLs, it comes with its own API. YOURLS provides bookmarklet, ajax feature and awesome statistics.

There’s a WordPress plugin available for YOURLS, making integration with your blog a snap: create short URLs and tweet them automatically as you publish blog posts.

YOURLS uses Arbitrary number base conversion library as mentioned above.


URL Shortener - LESSN

Lessn is an extremely simple, personal url shortener written in PHP with MySQL and mod_rewrite. It uses base_convert() function to generate hashes for short urls. It doesn’t have admin section.


URL Shortener - Phurl

Phurl is a PHP and MySQL based URL shortening script. It uses custom base conversion with 54 characters. Phurl can run with or without Apache mod_rewrite and has basic administration (URL deletion only).

3. Custom hash

For some reasons, you don’t like the auto-generated short url, you want a short but pretty url like: . This requires user input for hash, and the script have to check if the hash is in the database only.

Usually custom hash is used together with other methods above to give users the flexible way to get short urls. But there’s some script that provides only custom hash, that is Shorty.


URL Shortener - Shorty

Shorty provides admin section where you can monitor number of clicks for each url. You can edit or delete existing urls, too.

There is another type of hash generating that is the hash always has fixed length. Hash’s characters are chosen random from 62 characters. This is how URLi works. URLi uses 4 characters for hash, and it uses CodeIgniter:


URL Shortener - URLi

These are typical ways of generating short urls and some popular url shortening sources. I hope you enjoy this article. If you know other open sources, please let me know in the comments.

1 thought on “URL Shortener: Principles, Sample Code And Sources

  1. very cool on this!!
    $id = mysql_insert_id();
    $hash = base_convert($id, 10, 36);

    it save my life!

Comments are closed.


Subscribe to our newsletter to receive news and tutorials about WordPress and web development.

Memory - An elegant, clean and powerful WordPress blog theme! Learn more →