10

I'm building a new web app that has a requirement to generate an internal short URL to be used in the future for users to easily get back to a specific page which has a very long URL. My initial thoughts are to store a number in a database and output it in a HEXADECIMAL value to keep it shorter than an integer. TinyURL.com seems to use something other than HEXADECIMAL (multiple case letters mixed with numbers). Is there an easy way to generate something similar what TinyURL does?

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
ShawnCBerg
  • 594
  • 2
  • 7
  • 27
  • Not an answer to your question, but keep an eye on Shrinkr on Kazi Rashid's (Telerik fame) blog --> http://bit.ly/TgOcR -- his next post will be discussing shrinking logic – mikekidder Nov 04 '09 at 01:16
  • A side note: http://tr.im/ (another url shortening site) is going to be released/turned over to the public sometime next year. Check out the blog there. – eduncan911 Nov 04 '09 at 02:36

5 Answers5

7

Please, check out this good explanation on subject: Random TinyURL Browser (Updated) .

Important part:

As we have established, there are 62,193,780 possible values for TinyURL's. TinyURL's are generated by a Base 36 hash (36 indicating the number of characters a-z and 0-9, the array of possible values out of which a TinyURL can be constructed), autoincremented by MySQL with an initial value count of zero.

BTW, another SO similar question, through a mathematical view : Creating your own Tinyurl style uid. And here some .NET source code: Base 36 type for .NET (C#)

Community
  • 1
  • 1
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • Your link for explanation can be viewed by invitation only. Can you please provide some other link if possible? – skjoshi Nov 14 '14 at 17:17
  • 1
    @Sanju I don't have another link, but my point stands: encode your link id as a base36 value and you got your url shortener app. – Rubens Farias Nov 14 '14 at 20:37
2

They use base 36 encoding, and you can make your app more robust by using base 64.

Here's what I'd try in Python (I do see your language tags, forgive me):

#!/usr/bin/python

from base64 import b64encode
from hashlib import sha1

for i in range(5):
    salted_int = "<salt>%s</salt>" % i
    print b64encode(sha1(salted_int).hexdigest())[:6]

Outputs:

NTUwMz
ZTVmZD
OGEzNm
Njc2MT
YzVkNj

So you can autoincrement an integer and feed it to some kind of function like this, and end up with a good chance of a random group of strings. See also my answer to this question. Some base64 implementations have the potential to emit a slash / or a plus sign +, and therefore you should keep an eye out for these in your implementation as they're dangerous in URLs.

Hashes are really flexible and prevent your users from guessing the next URL (if this is important to you).

Community
  • 1
  • 1
Jed Smith
  • 15,584
  • 8
  • 52
  • 59
2

Another asp.net open-source one for you to investigate: mini url

tuanvt
  • 719
  • 4
  • 12
1

I recently saw something like this on codeplex for sharepoint and they seemed to use hexadecimal numbers for the url shortener. It might be worth taking a look at how they do it here http://spurlshortener.codeplex.com/

lomaxx
  • 113,627
  • 57
  • 144
  • 179
-1

My initial thoughts are to store a number in a database and output it in a HEXADECIMAL value to keep it shorter than an integer.

What is the point of keeping something shorter than an integer?
So you want to have URL like: http://here.there/12D687 instead of http://here.there/1234567?

If you'll ask me which one is easier for me I'll tell the latter one.
But honestly I do not see the point in my example as both are pretty much the same.

Is there an easy way to generate something similar what TinyURL does?

Yes. Ask the user to provide it.
If it is not possible just use the plain integer id. What can be easier...

Dmytrii Nagirniak
  • 23,696
  • 13
  • 75
  • 130