您应该将任何不可处理的请求重写为 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');
}