用户 ID 使用 HTaccess 自动更改为用户名


user id change automatically to username using htaccess?

嗨,我使用 htaccess 将profile.php?u=<username>更改为我的网站/用户名,当我在我的链接中使用它时,它运行良好,但我希望它在有人写profile.php?id=<uid>时自动更改

例如,在Facebook中,当您写"www.facebook.com/profile.php?id=4"时,它会变为"www.facebook.com/zuck"

我的访问代码

RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?u=$1 RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?u=$1

我可以添加什么?请帮忙。

没什么,因为您肯定没有可用于 Apache 的列表,其中包含您的用户 ID 到其各自用户名的映射。Facebook通过查询其数据库中的ID并将您重定向到包含从其数据库中检索的用户名的相应新URL,从而使用其PHP应用程序执行重定向。这样的 PHP 脚本可能如下所示:

<?php
$options = array("options" => array("min_range" => 1));
if (($id = filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT, $options)) {
    $user = new User($id);
    header("Location http://example.com/{$user->name}", true, 303);
    exit();
}