0

I've built a URL shortener using the Bijective Function algorithm as stated in Stack Overflow. My database table has two columns; ID and URL. For every input URL, the ID is automatically incremented its value is converted to base62. This value forms the slug for the shortened link.

Example: If ID is 42 then its base62 form is g (may differ according to the alphabet set). So the shortened URL is https://example.com/g.

Now I'd like to add custom URL support where the user chooses a custom URL slug. I could create another table to store custom URLs and then check both tables for a matching slug. But this seems rather inefficient.

Can anyone provide an efficient solution?

P.S. I'm using PHP and MySQL.

Community
  • 1
  • 1

1 Answers1

0

You could do the reverse mapping of the slug to the corresponding ID it would occupy if it was naturally generated, just keep this in mind:

Before increasing your ID variable you can make sure the entry it would increase to is not already populated by someone who entered a slug which corresponds to that ID, if that's the case just keep incrementing the ID column until you find a new unique value.

hbejgel
  • 4,674
  • 2
  • 17
  • 27