5

I have been wondering how tiny url works.

I would like to develop something similar for my site, but as most people, I use GUIDs for ids. When an object is created, should I then generate a 10 character random string to use as public id, or is there a smarter approach?

Example of old url: www.mysite.com/default.aspx?userId={id}

Example of new url: www.mysite.com/pwzd4r9niy

Dofs
  • 17,737
  • 28
  • 75
  • 123
  • 2
    Be careful not to allow collisions with the "real stuff" on your site! That would be bad. – Borealid Jul 07 '10 at 08:25
  • Borealid, what do you mean by collissions with 'real stuff'? – Dofs Jul 07 '10 at 08:32
  • Dofs: he means that if you generate a "new url" that is the same as a real page, you're going to run into trouble (imagine if your random string was `index.aspx` for example...). – Dan Puzey Jul 07 '10 at 08:35
  • Here's a [blog](http://ronny.haryan.to/archives/2009/04/07/build-your-own-url-shortening-service/) post about create a URL shortening service. Goes into a lot of detail about how it works. – Andy Robinson Jul 07 '10 at 08:33

2 Answers2

5

You can use any kind of random string generator or GUID for this. I don't think there is a much smarter approach. (Palantir offers a nice alternative though, hashing the incoming URL. )

The rest is relatively straightforward: Keep a database table with IDs and target URLs; When a request comes in, look up the ID and do a header redirect to the target URL.

More discussion in this blog post.

There also are redirection services out there now that use words from a dictionary list to build a URL.

Sadly, EvilURL is gone! It used to create "short" URLs like

http://evilURL.com/donkey_porn-shotguns/cracking-virus-exploit

that was the only URL redirection service really worthwhile. :)

And, as a bit of trivia, http://to is the shortest redirection service (and, I think the shortest web URL) known to man.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

Just hash the entire string, to a reasonable length.

Palantir
  • 23,820
  • 10
  • 76
  • 86