What is the best way to store a unique URL Slug?

泪湿孤枕 提交于 2019-11-30 10:08:11

I kinda like the way stackoverflow does it. Which is to put both the ID and the slug into the url. Now the slug no longer has to be unique. I believe hulu.com does it this way as well. I think it's a practical solution to the problem.

Most sites don't change the slug for the reason that if a user edits her title it shouldn't break any links to the post that have already been made.

For the database solution you already have, if it works well you can have an update trigger as well to update the slug if the content changes, but you should look for the performance hit.

For an alternate solution, you can use a map [loaded at start up containing existing slugs] that contains the key as the slug and existing count as value. Each time you generate a slug , determine the existing value and append the value higher than it in the table and thereafter update the map.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!