我需要重写从网址:有没有办法重写这个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
有没有办法做到这一点?
我需要重写从网址:有没有办法重写这个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
有没有办法做到这一点?
那么mod_rewrite无法查询您的数据库,因此它不能通过.htaccess本身完成。你可以做的就是有这个规则在.htaccess:
启用mod_rewrite的,并通过httpd.conf
的.htaccess,然后把这个代码在你.htaccess
下DOCUMENT_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;
}
伟大的解决方案,但如果htaccess无法查询数据库,那么像pinterest这样的网站如何具有这样的URL:http://pinterest.com/robingood/wow-collection/(robingood是用户名和哇,集合是类别)? – xRobot
@xRobot:即使有这样的URL方案。例如这个问题有这个URL:'http:// stackoverflow.com/questions/10992072/is-there-a-way-to-rewrite-this-url/10996636'现在尝试输入相同问题的简短URL'http://stackoverflow.com/questions/10992072',你会发现它被重定向到前一个URL。原因是它从数据库中提取问题标题并将其附加到较短的URL中以使其对搜索引擎友好。 – anubhava
在这种情况下,有问题的ID(10992072),但在pinterest的情况下没有。 – xRobot
你确定你不想要它吗? –
对不起,是的,我想要另一种方式。我只是正确的:D – xRobot