• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / Implement your own short URL

Implement your own short URL

October 7, 2008 by Sze Hau 5 Comments

Short URL or tiny URL is an URL used to represent a long URL. For example, http://tinyurl.com/45lk7x will be redirect to http://www.snippetit.com/2008/10/implement-your-own-short-url.

There are 2 main advantages of using short URL:

  • Easy to remember – Instead of remember an URL with 50 or more characters, you only need to remember a few (5 or more depending on application’s implementation).
  • More portable – Some systems have limitation on the total character to be used. For example SMS (short message services) and Twitter. When you want to send an 50 characters’ URL to a friend via SMS, you only left 90 characters for your message.

Implement your own short URL is simple as 1, 2 and 3:

  1. Define your own URL mapping algorithm.
  2. Have a database to store the mapped URL.
  3. Implement your URL mapping algorithm.

I’m not sure how the others create URL mapping algorithm, but at here, I will show my simple and fast’s short URL implementation. The following is the specification of my implementation:

The system uses 6 characters of short code to represent an URL of any length. The valid characters for the short codes are ASCII ‘a’ to ‘z’ and ‘0’ to ‘5’ where each character contains 2^5 (32) states. 6 characters of short code can used to map 32^6 (1,073,741,824) URLs so it is unlikely to be used up in the near future.

First, you will need a database to store and retrieve your mapped URL. Below is the suggested database schema:

CREATE TABLE mappedURL (
	shortCode	char(6) not null,
	lognURL		text not null,
	PRIMARY KEY 	shortCodeInd (shortCode),
);

Second, you will need to define an algorithm to map the long URL into short code. Below is the suggested algorithm:

loop1: while true
  calculate md5 of the URL
  loop2: from 1st 4 bytes to 4th 4 bytes of md5 result
    cast the 4 bytes to an integer
    loop3: for shortCodeChar[0] to shortCodeChar[5]
      use 1st 5 bits of the integer to find the value in codeMap
      remove 5 bits from the integer
    end loop3
    save shortCodeChar as shortCode
    if shorCode does not exist in database
      insert the short code and original URL into database
      break loop1:
    else
      retrieve the stored URL from database
      if original URL equals to URL stored in database
        break loop1:
      end if
    end if
  end loop2
  insert '-' at the front of the URL
end loop1
return shortCode

Note: codeMap contains value of valid characters from ‘a’ to ‘z’ (index 0 to 25) and ‘0’-‘5’ (index 26 to 31).

Third, you will need to create a page that receive a short code and redirect browser to the mapped URL in database:

if valid short code and exists in database
  redirect browser to the mapped URL
else
  show error or redirect to an error page
end if

And that’s all. You can use this implementation within your own domain as well to shorten the long URL of your blog before you send it to your friend. For example:

http://domain/long-long-long-long-url.html to http://go.domain/abcdef

Please note that there will be no source code provided in this page because you can implement it in any web languages (e.g. PHP, ASP or JSP) easily.

More from my site

  • Short URL: Top 5 Websites That Provide Free Short URL ServiceShort URL: Top 5 Websites That Provide Free Short URL Service
  • PHP: Short URL Algorithm ImplementationPHP: Short URL Algorithm Implementation
  • PayPal: Withdraw PayPal Money to Malaysia Bank AccountPayPal: Withdraw PayPal Money to Malaysia Bank Account
  • Unix Shell Script: How to Receive Input Right After Echoing A LineUnix Shell Script: How to Receive Input Right After Echoing A Line
  • How to Monetize Your Youtube VideoHow to Monetize Your Youtube Video
  • Blogger + Internet Marketer = REAL Full-time IncomeBlogger + Internet Marketer = REAL Full-time Income

Filed Under: Programming and Scripting, Tips and Tricks Tagged With: short URL, tiny URL

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Trackbacks

  1. How to map a long URL to a shorted one? - szehau’s weblog says:
    October 7, 2008 at 11:59 am

    […] http://www.snippetit.com/2008/10/implement-your-own-short-url/ […]

  2. PHP: Short URL Algorithm Implementation - Snippet IT says:
    April 10, 2009 at 7:48 pm

    […] monts ago, I introduced a simple algorithm that allow users to implement their own short URL into their system. Today, I have some spare time so I decided to write the short URL […]

  3. » Nace faja.me Quenerapú says:
    June 11, 2009 at 7:07 pm

    […] la posibilidad de montar mi propio recortador de URLs. Bien podía programarlo desde cero (aquí explican cómo), pero preferí dedicar algunos minutos a buscar por la red uno prefabricado con código libre o […]

  4. Short URL: Top 5 Websites That Provide Free Short URL Service | Snippet IT says:
    December 10, 2009 at 1:57 am

    […] What is a "short URL"? Short URL or tiny URL is an URL used to represent a long URL For example, http://tinyurl.com/45lk7x can be used to represent and will be used to redirect to http://www.snippetit.com/2008/10/implement-your-own-short-url. […]

  5. Implement short url using C# | 正直紳士們的社交場 says:
    March 11, 2012 at 11:52 am

    […] Implement your own short URL […]

Leave a Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in