1

我需要从以下位置重写 URL:

www.example.com/John.Connor/my-first-post/42

至:

www.example.com/post.php?postid=42&userid=19

表:

USER:
id, name, surname, email, password

POST:
id, title, post, userid

有没有办法做到这一点 ?

4

1 回答 1

1

那么 mod_rewrite 无法查询您的数据库,因此它不能完全通过 .htaccess 本身完成。你可以做的是在 .htaccess 中有这个规则:

通过启用 mod_rewrite 和 .htaccess httpd.conf,然后将此代码放在您.htaccessDOCUMENT_ROOT目录下:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^[^/]+/[^/]+/([0-9]+)/?$ post.php?postid=42 [L,QSA]

然后在你的post.php脚本里面有这样的代码:

$postid = mysql_real_escape_string($_GET['postid']); 
$userid = mysql_real_escape_string($_GET['userid']);

if (empty($userid)) {
   $userid = mysql_result(mysql_query("SELECT userid FROM POST WHERE id=$posid"), 
              0, "userid");
   // now redirect by appending &userid=49 in the current URL
   header("Location: " . $_SERVER["REQUEST_URI"] . "&userid=" . $userid);
   exit;
}
于 2012-06-12T12:26:27.610 回答