您應該重寫任何不可服務的請求到PHP腳本來處理這個問題,然後您可以對數據做任何事情。
在你的.htaccess添加(後你在的地方有任何其他重寫規則):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /404.php?uri=$0 [L]
而且在404.php
做這樣的事情(半僞代碼):
// Get the redirect mapping for this URI from the database
$query = "
SELECT location
FROM redirects
WHERE uri = ?
";
$redirect = $db->query($query, $_GET['uri']);
if ($redirect) {
// Update database with tracking data
$query = "
UPDATE redirects
SET
hits = hits + 1,
lastHit = NOW()
WHERE uri = ?
";
$db->query($query, $_GET['uri']);
// Do the redirect
header($_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently');
header('Location: ' . $redirect);
} else {
// Really not found
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
}
'.htaccess'不僅應該小到可讀性,還要服務器優化。使用php或cgi來完成這項工作有什麼想法? – 2013-04-11 11:09:50