0

I've read multiple posts of SO regarding the topic.

How to code a URL shortener?

How do URL shortener calculate the URL key? How do they work?

PHP URL Shortening Algorithm

Every posts recommends to store the url in the database. It will return you the id, pass the id to a hash function, returning a tiny id.

My question is what will happen if the same url is requested to shorten it again? Bitly.com returns the same tiny url again for the same url.

What exactly should be the best way to go forward in order to ensure non-duplicate urls??

Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
  • 2
    Why would you _not_ return the same shortened URL for two identical originals? – 500 - Internal Server Error Aug 27 '17 at 22:45
  • When somebody enters a url to be shortened, look that url up in the database. If it's already been shortened, then just return the existing tiny url. If not, create a new tiny url and put it in the database. – Jim Mischel Aug 28 '17 at 02:54
  • 1
    Thats a full table scan. For millions of records that will take a long time. I can't even index the TEXT field. Any way to optimize this? – Praful Bagai Aug 28 '17 at 04:54
  • The above shared posts have different tinyurls for same long url. Their algorithm just creates a new id everytime a url needs to be shorten, irrespective of the duplicates. – Praful Bagai Aug 28 '17 at 04:57
  • Any idea about the optimization part? – Praful Bagai Aug 28 '17 at 11:44

0 Answers0