你會使用一個的.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