没有 id 的漂亮链接

发布于 2024-08-07 00:07:29 字数 479 浏览 7 评论 0原文

我正在考虑为新网站实现漂亮链接的选择。

我了解如何使基础知识发挥作用,例如执行:

/view/postTitle/7895

我可以只选择 id 7895 并忽略标题。

然而,我发现一些网站设法完全摆脱 ids:

/view/postTitle

这是如何工作的?

我能想到的唯一方法是,在每个请求中,pageTitle 作为字符串发送,并且一些代码执行一些 SQL 检查以返回正确的 id。然而,这将意味着大量潜在昂贵的 SQL 查询,不是吗?尤其是当你有很多帖子的时候。

另外,这意味着每次有人创建帖子时,我都需要为该帖子生成一个唯一的、URL 友好的字符串。我需要检查数据库,直到获得唯一的数据库。这一切似乎太多了不是吗?

我这样看是不是错了?或者是否有现成的 API 和工具可以让这一切变得更容易、更高效?

我正在使用 PHP/MySQL、Apache。

谢谢

I'm considering my options to implement pretty links for a new website.

I understand how to make the basics work, for example doing:

/view/postTitle/7895

I can just pick up the id 7895 and just ignore the title.

However, I see that some websites manage to get rid of ids altogether:

/view/postTitle

How does this work?

The only way I can think of is that at every request, the pageTitle is sent as a string and some code does some SQL checking to return the correct id. However that would mean a lot of potentially expensive SQL queries no? Especially when you have a lot of posts.

Also, it means that every time someone creates a post, I need to generate a unique, url-friendly, string for that post. I need to check against the database until i get a unique one. That all seems like too much no??

Am I looking at this wrong way? Or are there ready APIs and tools to make this easier and more efficient?

I'm using PHP / MySQL, Apache.

Thanks

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

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

发布评论

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

评论(3

那小子欠揍 2024-08-14 00:07:29

创建一个对帖子标题进行规范化/清理的函数 - 一种简单的方法可能是对原始标题的 urlencode() 表示进行哈希处理。将其存储为“id”而不是数据库生成的数值,并且您的 SQL 不需要进行重大更改。

Create a function that normalizes/sanitizes post titles - an easy way might be to hash the urlencode() representation of the original title. Store that as the 'id' instead of a db-generated numerical value, and your SQL doesn't need to significantly change.

懷念過去 2024-08-14 00:07:29

你是对的。您必须处理两个方面。

首先,当有人提交帖子时,您可以生成一个 html 友好的 URL,用于添加到每个链接。实际上,这并不太难。您需要替换空格、特殊字符等。基本上,您为数据库存储所做的任何转义都是一个很好的起点。您还需要确保这些是唯一的。

其次,当有人通过链接访问您的网站时,您需要正确解决它。这个有点复杂,但如果您将每个唯一生成的 URL 视为一个 id,则可以有效地工作,因为这本质上就是它的结果...如果您对其进行查询(转义当然!)。

为了实现可靠的实施,大多数 CMS(例如 WordPress 和 Drupal)都可以非常有效地处理这个问题。来自 Drupal 背景,我建议您查看“path”和“pathauto”模块,了解如何完成此操作,重点关注“path”......它涵盖了问题的两部分。

You are correct. There are two aspects that you have to handle.

First, when someone submits a post, you have a generate a html-friendly URL for adding to each of the links. Realistically, this isn't too hard. You need to replace spaces, special characters, etc. Basically, whatever escaping that you're doing for database storage is a good starting point. You'll also need to make sure these are unique.

Second, when someone hits your site with the link, you need to resolve it properly. This one is a bit more complex but can effectively work the same if you think of each uniquely generated URL as an id because that's essentially what it becomes... if you query on it (escaped of course!).

For a solid implementation, most of the CMS's - WordPress and Drupal come to mind - handle this pretty effectively. Coming from a Drupal background, I would suggest that you look at the "path" and "pathauto" modules for how this is done with a focus on "path"... it covers both halves of the problem.

后知后觉 2024-08-14 00:07:29

你有正确的方法。
通常,如果它是一个带有帖子的简单 CMS,请使用 WordPress 等软件包,并根据您的需要进行修改。
如果您有自定义数据类型,我强烈推荐 SilverStripe,如果您想使用用户,请使用 Drupal。
有一些库和函数可以将帖子标题转换为 url,这也是一个值得研究的好主意。
开源很棒,你可以看看别人是怎么做的,尝试搜索 github 之类的。

You have the right approach.
Typically, if it's a simple CMS with posts, use a package like WordPress, and modify it to your needs.
If you have custom datatypes, I highly recommend SilverStripe, and if you want to use users, use Drupal.
There are libraries and functions floating around that can convert the post title to a url, which would also be a good idea to look into.
Open-Source is great, and you can look at how others have done it, try searching github and the likes.

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