How do I create permalinks that are database driven?

我的未来我决定 提交于 2021-02-08 06:14:36

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5018114/how-do-i-create-permalinks-that-are-database-driven

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