mod_rewrite 和 Pretty url 背后的概念是什么?

发布于 2024-09-17 05:58:34 字数 390 浏览 2 评论 0原文

我用谷歌搜索了很多,阅读了很多,但仍然无法弄清楚漂亮的 url 和 mod_rewrite 背后的基本概念。

我目前正在以 php 方式制作漂亮的 url。像这样

a)在表中创建一个字段,其名称与文本(或我想要在url中的标题)相同,并用(-)分隔。

b) 使用相同的字段值创建超链接,例如 (a href="my-page-name")

c) 当通过查询字符串传递时,检查数据库中的值,如果匹配,则获取该行的 id 并执行其他操作...

到目前为止它正在工作..但我很困惑。

这是正确的方法吗?或者它可以通过 .htaccess 进行控制,因为我在所有需要漂亮 URL 的页面中都做了同样的事情。

请有人指导我..

感谢您的阅读;)

I have googled a lot, read a lot but still cannot figure out the basic concept behind pretty url and mod_rewrite.

I am currently making pretty url the php way. Like this

a) creating a field in a table with the name same as the text (or the title which i want in the url) separated with (-).

b) making hyperlink with the same field value like (a href="my-page-name")

c) and when passed through querystring checking the value from the database and if it matches, get the id for that row and do other stuffs...

So far it is working.. but I am confused.

IS THIS THE RIGHT WAY ? OR it can be controlled through .htaccess, since I am doing the same thing in all the pages which needs pretty URL.

Someone guide me please..

Thanks for reading ;)

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

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

发布评论

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

评论(1

2024-09-24 05:58:34

实现由你决定,.htaccess 将包含匹配请求的 URL 的一部分并将其传递给你的脚本的规则,然后由你决定做你想做的事情,通常在使用 url 重写时保留 id,但他们添加了标题的“干净”版本,例如:

.htaccess 将匹配 id (1) 和干净的标题 (my-first-post),并且将它们传递到您的页面,该页面反过来应该获取参数,获取具有该 id 的页面并显示它。通过这种方式,页面可以利用漂亮的 URL,同时又不会失去快速的 ID 查找功能。


注意:

  • 在使用上述示例时,建议您检查给定的 clean-title 是否与您实际拥有的相同(您可以将其缓存在 posts 表中的字段中),如果不同,您应该将用户重定向到具有真实标题的页面。示例:http://example.com/blog/1-wrong-title 将重定向到 http://example.com/blog/1-my-first-post >。
  • 这只是一个建议的实现,如果你希望忽略url中的id,并依赖缓存的clean-title表字段来查找文章,你当然应该考虑对该字段进行唯一索引对于快速查找,这种方法的一个例子是维基百科(Mediawiki)。

The implementation is up to you, .htaccess would contain rules that would match a part of the requested URL and pas it to your script, and then it's up to you to do what you want, usually when using url rewriting the id is retained, but they add the "clean" version of the title to it, like:

the .htaccess will match both id (1) and clean title (my-first-post), and pass them to your page, which in turn should grab the parameters get the page with that id and display it. This way page uses takes advantage of pretty URLs without losing the quick id lookup.


Notes:

  • When using the mentioned example, you are advised to check whether the given clean-title is actually the same you have (you may cache that in a field in the posts table), if it was different, you should redirect the user to the page with the real title. Example: http://example.com/blog/1-wrong-title would redirect to http://example.com/blog/1-my-first-post.
  • It's just a suggested implementation, if you want you can ignore the id in the url, and depend on the cached clean-title table field to lookup the article, you should of course consider uniquely-indexing that field for fast lookups, an example of that approach is Wikipedia's (Mediawiki).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文