want to create a URL shortener service where you can write a long URL into an input field and the service shortens the URL to "http://www.example.org/abcdef
". Instead of "abcdef
" there can be any other string with six characters containing a-z, A-Z and 0-9
. That makes 56~57 billion possible strings.
Edit: Due to the ongoing interest in this topic, I've uploaded the code that I used , with implementations for , and . Add your solutions if you like :)
My approach:
I have a database table with three columns:
- id, integer, auto-increment
- long, string, the long URL the user entered
- short, string, the shortened URL (or just the six characters)
I would then insert the long URL into the table. Then I would select the auto-increment value for "id
" and build a hash of it. This hash should then be inserted as "short
". But what sort of hash should I build? Hash algorithms like MD5 create too long strings. I don't use these algorithms, I think. A self-built algorithm will work, too.
My idea:
For "http://www.google.de/
" I get the auto-increment id 239472
. Then I do the following steps:
short = '';if divisible by 2, add "a"+the result to shortif divisible by 3, add "b"+the result to short... until I have divisors for a-z and A-Z.
That could be repeated until the number isn't divisible any more. Do you think this is a good approach? Do you have a better idea?
encode()
anddecode()
functions. The steps are, therefore: (1) Save URL in database (2) Get unique row ID for that URL from database (3) Convert integer ID to short string withencode()
, e.g.273984
tof5a4
(4) Use the short string (e.g.f4a4
) in your sharable URLs (5) When receiving a request for a short string (e.g.20a8
), decode the string to an integer ID withdecode()
(6) Look up URL in database for given ID. For conversion, use: – Feb 10 '15 at 10:31