Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 190013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:13:13+00:00 2026-05-11T16:13:13+00:00

I want to create a URL shortener service where you can write a long

  • 0

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

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 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 short
if 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?

Due to the ongoing interest in this topic, I’ve published an efficient solution to GitHub, with implementations for JavaScript, PHP, Python and Java. Add your solutions if you like 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:13:13+00:00Added an answer on May 11, 2026 at 4:13 pm

    I would continue your “convert number to string” approach. However, you will realize that your proposed algorithm fails if your ID is a prime and greater than 52.

    Theoretical background

    You need a Bijective Function f. This is necessary so that you can find a inverse function g(‘abc’) = 123 for your f(123) = ‘abc’ function. This means:

    • There must be no x1, x2 (with x1 ≠ x2) that will make f(x1) = f(x2),
    • and for every y you must be able to find an x so that f(x) = y.

    How to convert the ID to a shortened URL

    1. Think of an alphabet we want to use. In your case, that’s [a-zA-Z0-9]. It contains 62 letters.
    2. Take an auto-generated, unique numerical key (the auto-incremented id of a MySQL table for example).

      For this example, I will use 12510 (125 with a base of 10).

    3. Now you have to convert 12510 to X62 (base 62).

      12510 = 2×621 + 1×620 = [2,1]

      This requires the 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 map the indices 2 and 1 to your alphabet. This is how your mapping (with an array for example) could look like:

      0  → a
      1  → b
      ...
      25 → z
      ...
      52 → 0
      61 → 9
      

      With 2 → c and 1 → b, you will 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 do a reverse lookup in your alphabet.

    1. e9a62 will be resolved to “4th, 61st, and 0th letter in the alphabet”.

      e9a62 = [4,61,0] = 4×622 + 61×621 + 0×620 = 1915810

    2. Now find your database-record with WHERE id = 19158 and do the redirect.

    Example implementations (provided by commenters)

    • C++
    • Python
    • Ruby
    • Haskell
    • C#
    • CoffeeScript
    • Perl
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create URL re-write rules for following URL in htaccess. http://example.com/vedios/category/fun/ ->
I want to create a url like www.facebook.com/username just like Facebook does it. Can
I want to create a simple URL shortner that would do the following: http://example.com/code
I want to create a url as below http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false I used the following code
I want create API which has same URL say http://stackoverflow.com/profile but having different request
I have url like: http://www.matweb.com/search/DataSheet.aspx?MatGUID=849e2916ab1541be9ff6a17b78f95c82 I want to download source code from that page
I want to write my own URL shortener using php and mysql as a
Need Help, I want create an image access caching, example image at url example.com/caching/m_images.jpg
I want to write a url shorten service. I trying to think of scenarios
I want to create a private url as http://domain.com/content.php?secret_token=XXXXX Then, only visitors who have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.