PHP/MySQL 完整的文件名而不是从 ID 中抓取

PHP/MySQL full file name instead of grabbing from ID

我一直想知道如何设置 php,这样您就可以像 www.mysite.com/index.php?id=1 你通过 www.mysite.com/pages/news-1.php

得到它们

我不知道还有什么办法可以弥补这个问题;但是我是否必须手动创建新页面并将它们放入目录中,然后通过 db link page/ID 或者是否有另一种方法仅使用 mysql。

使用 URL 重写,您需要将所有页面请求路由到您的 "front controller" / index.php。然后您使用路由器将该请求发送到特定的控制器方法。在该方法中,您将获取请求的路径(例如 /postname)。您的帖子数据库 table 将有一个 "slug" 字段。然后,您在数据库中查询包含 slug "postname".

的 "posts" 结果

请查看本教程以了解如何制作自定义框架,它会为您提供大量背景信息,可能会阐明这一切的工作原理:http://symfony.com/doc/master/create_framework/index.html

在您的网站根目录中创建一个名为 .htaccess 的文件。并将这些行粘贴到其中 (Tested):

RewriteEngine On    # Turn on the rewriting engine    
RewriteRule    ^pages/news-([0-9]+)/?$     index.php?id=    [NC,L]

URL rewriting can be one of the best and quickest ways to improve the usability and search friendliness of your site. It can also be the source of near-unending misery and suffering. Definitely worth playing carefully with it - lots of testing is recommended. With great power comes great responsibility, and all that. Aug 4, 2008 - addedbytes.com