1

Due to some issues with other shorteners like goo.gl (disabling my links for example) I want to create my own URL shortener.

I am looking to have a single table that will contain the following columns :-

links_id - autoincrement id
url - the actual full URL
abbreviation - the shortened version 

In a nutshell, when a new link is added to the table, I will insert the URL into the table and give this a unique abbreviated value, obviously if an existing URL is found it won't need to re-add the URL.

My question is what is the best way to generate such abbreviations that a) are fast to produce and are as unique as possible and not simple to guess. In addition how many number of characters would people recommend, for instance if I had an abbreviation of 6 characters how many unique combinations would this provide me based I am using the standard characters as used by other URL shorteners.

I will be using PHP/MySQL, any advice would be appreciated.

  • 4
    It is impossible to generate unique hashes, that is the very nature of hashes. – Jörg W Mittag Jan 6 '17 at 12:17
  • 3
    You're operating under the assumption that the identifiers used by URL shorteners are hashes. They aren't. – Blrfl Jan 6 '17 at 12:21
  • 1
    Why is "not simple to guess" a requirement? And what do you mean by that; that if I have shortened URL AAAB1 do you want me to not be able to know URL AAAB2 probably exists and was created shortly after mine. Or do you want me to not be able to know that AAAB2 is actually Google.com? – Richard Tingle Jan 6 '17 at 13:02
  • 1
    Before the nitpickers come and close the question, I took the freedom and replaced the word "hash" by "abbreviation". – Doc Brown Jan 6 '17 at 13:14
  • @JörgWMittag Long hashes (SHA256 or better) can be treated as unique for all purposes. But of course that doesn't work for a url shortener. – CodesInChaos Jan 6 '17 at 15:30
7

URL shortners do not use hashes to generate the shortened URL. Hashes are not (by their very design) unique.

A common way to encode URLs is to simply take the links_id value and convert it to base 62 (ie a-z, A-Z and 0-9, giving 62 values per "digit") and use that as the URL.

  • Long hashes (SHA256 or better) can be treated as unique for all purposes. The utter unfeasability of ever finding two different inputs with the same hash is their most important design property. But of course that doesn't work for a url shortener. – CodesInChaos Jan 6 '17 at 15:32
  • @CodesInChaos, You are of course correct. But I tend to think of those long hashes as GUIDS & do shorthand GUID = unique; hash = non-unique & for eg hash tables. – David Arno Jan 6 '17 at 15:34
  • Concerning the "not simple to guess", the links_id might not be the perfect solution, especially with auto_increment in MySQL. A script could easily extract all URLs from the shortener. Depending on the use case, this might not be intended. – Trendfischer Jan 6 '17 at 15:48
  • @Trendfischer: this is probably a red herring, see the Richard Tingle's comment below the question. As long as the OP does not clarify this, I would assume he has not really thought about what he asking for here. – Doc Brown Jan 6 '17 at 16:03
  • @DocBrown Sorry, I've seen the comment too late, made my answer to this below the question. – Trendfischer Jan 6 '17 at 16:13
3

The idea on creating an URL shortener is using short strings to abbreviate the URL. Most common hashing functions create a long hex decimal string, which is longer than necessary, even abbreviated. I would go with the following design:

  • Query the database, if the URL is already existing, return the saved short string
  • Create a random number the size approximate on how many URLs you will expect in your database
  • Convert the number to a base64 string or base36 (using a-z instead a-f, e.g. see the function base_convert for php), do not forget to replace URL specific characters on a base64 encoding
  • Run a query against your database to test, if the random string is already in your database, if yes, repeat the generation
  • Save the link in your database with the generated random short string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.