dongzhangji4824 2017-02-02 13:48
浏览 63
已采纳

为我的URL缩短器添加自定义URL支持

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.

  • 写回答

1条回答 默认 最新

  • dongqiang5541 2017-02-02 20:16
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!