存储唯一 URL Slug 的最佳方式是什么?

发布于 2024-07-11 10:54:35 字数 500 浏览 9 评论 0原文

我正在尝试为我的网站生成一些 url 'slugs' 。 它基于用户生成的单段文本。

现在,我已经制作了自己的 slug 方法,所以我不需要一些代码。

我想知道的是确定该 slug 是否唯一然后插入它的最佳位置在哪里,因为 slug 字段是唯一键索引。

最初,我对任何插入(针对表)都有一个触发器,因此当输入数据时,就会确定段。 我有一个函数,用于检查包含用户文本(不是 slug)的记录数,然后生成 slug,并将记录计数 + 1 添加到新 slug 的末尾。

例如。

在表中找到 5 条具有相同用户生成内容的记录。 现在的 slug 是 slug-text,末尾添加了 6。

缺陷:如果用户更改文本,则 slug 不会更改。

无论如何,我想知道其他人之前是否解决过这个问题并找到了解决方法?

I'm trying to generate some url 'slugs' for my website. It's based upon a single piece of user generated text.

Now, i have made my own slug method, so i'm not after some code for that.

What i'm wondering is where is the best place to determine if this slug is unique and then insert it because the slug field is a Unique Key Index.

Originally, i had a trigger on any insert (against the table) so when the data is entered, the slug is then determined. I had a function that checked for the number of records that contained the user's text (not the slug) and then generated the slug and added the record count + 1 to the end of the new slug.

eg.

5 records are found in the table with the same User generated content.
the slug for this now the slug-text with a 6 added to the end.

Flaws: if the user changes their text, the slug doesn't change.

Anyways, i'm wondering if other people have delt with this issue before and found any ways to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流星番茄 2024-07-18 10:54:35

我有点喜欢 stackoverflow 的做法。 即将 ID 和 slug 都放入 url 中。 现在,蛞蝓不再必须是唯一的。 我相信hulu.com也是这样做的。 我认为这是解决问题的实际方法。

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.

等你爱我 2024-07-18 10:54:35

大多数网站不会更改别名,因为如果用户编辑其标题,则不会破坏任何已发布帖子的链接。

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.

云归处 2024-07-18 10:54:35

对于您已有的数据库解决方案,如果它运行良好,您也可以有一个更新触发器,以便在内容发生更改时更新 slug,但您应该寻找性能影响。

对于替代解决方案,您可以使用映射[在启动时加载,包含现有的 slugs],其中包含作为 slug 的键和作为值的现有计数。 每次生成 slug 时,请确定现有值并将高于该值的值附加到表中,然后更新地图。

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.

过度放纵 2024-07-18 10:54:35

您可以使用每个帖子的 slugs 集合(带有帖子链接的单独表格)并确定实际当前的帖子。 您的帖子应该存储当前帖子的 ID,例如将其作为规范用于 SEO。 它还将受益于更改 slug 以进行更好的 SEO 优化。 如果您不想使用 SEO - 您可以做任何您想做的事情(只需存储一个 slug),这会很好。 有很多策略可以实现其独特性。
要通过索引在数据库中执行相同 slug 的快速搜索,请使用“my_slug%”,并从结果中构建唯一的一个(例如添加帖子的 id)。 如果结果为空 - 继续保存。

但如果您使用 slugs 集合 - 不要忘记使这些 url 可从互联网访问

You can use collection(separate table with link to post) of slugs for each post and determine one that is actually current. Your post should store id of current post, for example to use it for SEO as canonical. It will also benefit from changing slug to make better SEO optimization. If you don't want to use SEO - you can do whatever you want (just store a single slug) and it will be good. There is a lot of strategies to reach its uniquences.
To perform quick search of the same slug in DB by index use 'my_slug%' and from results build the unique one (for example add id of post). If result is empty - proceed with saving.

But if you use collection of slugs - don't forget to make those url to be reachable from the internet

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文