[Solved]-Developing a URL Shortener


  1. I do not understand how can I create unique strings for each long URL to use as a short URL. Something like other popular URL shorteners do.
    As sugerman has said, this is simple, you just create a hash table.

  2. How can I do this?
    There are dynamic ways to do this, but the simplest and most effective is to have a 2 field table in a database, which holds the hashkey and full url. Then your server, like Apache, would have the ability to redirect to the correct page.

  3. Is it possible to make all short urls of the same length?
    Yes, to a certain extent, however once you reach the maximum amount of keys, you would have to reuse/replace the short url IDs. When you set a fixed-length, then you’re limiting the amount of possibilities.

My question to you:

I’m under the assumption that by URL shortener you are referring to something like jsFiddle or a pastebin in that they have something like http://jsfiddle.net/sdfj2/. Otherwise, we’d need some more clarification.


You’ll probably want to create a simple db table mapping a short value to a URL.

The simplest short url you can generate would just be a serial number or autoincrement column (assign the first value 1, then 2 and so on)

It’s possible to make all of the URLs of the same length as long until you don’t run out of values of the same length, eg if you were using only numbers (as a simple example) it would be from 0000 to 9999.

The magic part would be where you would use mod_rewrite to pass the url to your script as a parameter, have your application look the value up in the db then redirect the user.

This rewrite rule for mod_rewrite will take a URL like example.com/0000 and redirect to example.com/index.py?id=0000 You would put this in .htaccess (I’m assuming you’re using apache.)

Your application just reads the id and redirects to the associated page.

RewriteRule ^([0-9]+)/?$ index.py?id=$1 [QSA,L]

If you decide to go with a hash, or a simple base64 serial number (which would be a more compact autoincrement), it would just look slightly different:

RewriteRule ^(.*)/?$ index.py?id=$1 [QSA,L]


I wrote the blog post linked above. basically, how I did it was as follows:

Store the long url in a table with an auto-incrementing index field. Retrieve the index. Convert it to a string, the same way you would convert it to say, a hexadecimal or octal number, but using all available characters as possible “digits”. For example, say we have a-z, A-Z, and 0-9 available, do the following:

0 = 0
1 = 1

9 = 9
10 = a
11 = b

35 = z
36 = A
37 = B

60 = Z
61 = 10
62 = 11

70 = 19
71 = 1a
72 = 1b

You get the pattern? It’s a generic conversion algorithm from a base-10 number to a base-n number, where n is the number of characters to your disposal.


You’re probably going to want to set up a hash function to generate the shortened URL.


Rather than sit here and summarize a pretty detailed Wikipedia article for you, here’s a link to a blog post explaining how hash functions work in relation to your exact topic (URL shorteners).


Check django-shorturls app.



Here is my take on it: https://github.com/bitmazk/django-tinylinks
One client is using this in production since one year.

Leave a comment