你会使用一个的.htaccess文件所有请求传递到您的前端控制器(通常只是一个的index.php脚本),然后该脚本在数据库中的传入请求中的记录匹配。
举例来说,如果你有一个数据库表叫pages
四列:id
,title
,slug
和content
,下面是一个简单的实现......
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1 [NC,L]
这告诉Apache将采取所有请求是不是文件或方向,并将它们发送到index.php。
你的index.php那么可以看看如下:
<?php
// Take request URI
// Would be something like 'your-slug'
$request = trim($_SERVER['REQUEST_URI'], '/');
// Set up database connection and attempt to match slug
$sql = "SELECT * FROM pages WHERE slug = ? LIMIT 1";
$smt = $db->prepare($sql);
$smt->execute(array($request));
$page = $smt->fetchObject();
if (! $page) {
// Page was not found matching slug
header('HTTP/1.1 404 Not Found');
exit;
}
// Display matching page in a template
从这里,你就可以建立在它。
可能重复的[漂亮的URL在PHP框架](http://stackoverflow.com/questions/8440490/pretty-urls-in-php-frameworks) – mario
可能重复的[漂亮的URL的Web应用程序](http: //stackoverflow.com/questions/5142095/pretty-urls-for-web-application) – mario