I want to create a URL shortener service where can write a long URL into an input field and the service shortens the U RL to " http://www.example.org/abcdef
". Instead of " abcdef
" "there can is any and string with six characters containing a-z, A-Z and 0-9
. That makes 56~57 billion possible strings.
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 is should then is 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 would 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 is repeated until the number isn ' t divisible any more. Does think is a good approach? Do you had a better idea?
Edit: Due to the ongoing interest-topic, I ' ve uploaded the code that I used to GitHub, with implementations for Java, PH P and JavaScript. ADD Your solutions If you like :)
I would continue your "convert number to string" approach. However you'll realize that your proposed algorithm fails if your ID is a prime and greater than.
Theoretical background
You need a bijective Function f. This is necessary so, can find a inverse functiong (' abc ') = 123 for your f (123) = ' abc ' function . This means:
- There must is no x1, X2 (with x1≠x2) that would make f (x1) = f (x2),
- And for every y must is able to find a x so , f (x) = y.
How to convert the ID to a shortened URL
- Think of an alphabet we want. In your case that ' s
[a-zA-Z0-9]
. It contains letters.
- Take an auto-generated, unique numerical key (the auto-incremented of
id
a MySQL table for example).For this example I'll use the 12510 (with a base of 10).
- Now there are to convert 12510 to X62 (base 62).
12510 = 2x621 + 1x620 =[2,1]
This requires use of integer division and modulo. A Pseudo-code Example:
digits = []while num > 0 remainder = modulo(num, 62) digits.push(remainder) num = divide(num, 62)digits = digits.reverse
Now maps the indices 2 and 1 to your alphabet. This is what your mapping (with a array for example) could look like:
0 → a1 → b...25 → z...52 → 061 → 9
With 2→c and 1→b you'll receive cb62 as the shortened URL.
http://shor.ty/cb
How to resolve a shortened URL to the initial ID
The reverse is even easier. You just does a reverse lookup in your alphabet.
- E9a62 'll be resolved to "4th, 61st, and 0th letter in Alphabet".
E9a62 = [4,61,0]
= 4x622 + 61x621 + 0x620 = 1915810
- Now find your database-record with and do the
WHERE id = 19158
redirect.
Some implementations (provided by commenters)
- Ruby
- Python
- Coffeescript
- Haskell
- C#
How to code a URL shortener?