如何创建数据库驱动的永久链接?
我正在寻找一种翻译
http://www.mysite.com/articles 的方法。 php?id=1
到
http://www.mysite.com/articles/name_of_article_from_field_in_database_with_id_1
我可以用 Mod Rewrite 进行某种 RewriteRule,但我不确定如何准确地做到这一点。我的页面是用 PHP 编码的,从 MySQL 数据库中提取。
希望有人能告诉我该怎么做!
我知道还有其他人在寻找类似的答案。
再次感谢!
NC编码器
I'm looking for a way to translate
http://www.mysite.com/articles.php?id=1
into
http://www.mysite.com/articles/name_of_article_from_field_in_database_with_id_1
I'm ok with doing some sort of RewriteRule with Mod Rewrite, but I'm not sure how to do it exactly. My pages are coded in PHP pulling from a MySQL database.
Hopefully someone can tell me how to do this!
I know there are others looking for a similar answer.
Thanks again!
NCoder
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在 Web 服务器配置中使用重写规则,将例如
/123
转换为/index.php?id=123
- 这本身不是 PHP 问题。You're going to need a rewrite rule in your web server configuration to translate e.g.
/123
to/index.php?id=123
- it's not a PHP issue per se.以下所有内容都可以通过 apache 重写来实现。您可以找到大量有关如何执行此操作的教程和文档。
为了使永久链接看起来漂亮且 SEO 友好,您可以遵循 example.com/article/how-to-do-stuff 模式作为标题为“How to do stuff”的文章的链接。然而,如果您的网站将收到很多文章,由于数据库中的文本搜索,速度会稍微慢一些(这将是一个很难达到的水平,但让我们谈谈假设)。
因此,我建议使用 phpBB 模式 example.com/article/12345-how-to-do-stuff,其中 12345 是数据库中模式的 id(或任何其他足以从数据库中选择的足够快的 id) 。这还不是全部。 “应该”还有一个验证步骤,将 URL 中的文本与所选文章的标题进行比较,最终重定向到正确的 URL。
All the below stuff can be implemented with apache rewrites. You can find a ton of tutorials and documentation on how to do this.
In order to make permalinks look nice and SEO friendly, you can follow the pattern example.com/article/how-to-do-stuff as a link for the article entitled "How to do stuff". However if your site will get many articles, things will slow down a little bit because of text search in the database (that will be a hard to achieve level, but let's talk hypothetical).
As a result I'd recommend using the phpBB pattern example.com/article/12345-how-to-do-stuff where 12345 is the id of the pattern in the database (or any other id that's fast enough to select from db). That's not all. There 'should' be one more verification step, where the text from the URL, is compared with the title of selected article, and an eventual redirect to the correct URL will be done.