How to code a URL shortener?

Source: Internet
Author: User
Tags integer division

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 " ". 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:

    1. ID, Integer, auto-increment
    2. Long, string, the long URL the user entered
    3. 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 " 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
  1. Think of an alphabet we want. In your case that ' s [a-zA-Z0-9] . It contains letters.
  2. 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).

  3. 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.

How to resolve a shortened URL to the initial ID

The reverse is even easier. You just does a reverse lookup in your alphabet.

    1. E9a62 'll be resolved to "4th, 61st, and 0th letter in Alphabet".

      E9a62 = [4,61,0] = 4x622 + 61x621 + 0x620 = 1915810

    2. 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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.