.htaccess 有没有办法重写这个URL?
我需要从以下位置重写URL:.htaccess 有没有办法重写这个URL?,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我需要从以下位置重写URL: www.example.com/John.Connor/my-first-post/42 致: 表: USER: id, name, surname, email, password POST: id, title, post, userid 有没有办法做到这一点?嗯,mod_rewrite无法查询您的数据库,因此无法完全通过.htaccess本身完成。您可以在.htaccess中设置此规则: 通过httpd.conf启用mod_rewrite和.htacce
www.example.com/John.Connor/my-first-post/42
致:
表:
USER:
id, name, surname, email, password
POST:
id, title, post, userid
有没有办法做到这一点?嗯,mod_rewrite无法查询您的数据库,因此无法完全通过.htaccess本身完成。您可以在.htaccess中设置此规则: 通过
httpd.conf
启用mod_rewrite和.htaccess,然后将此代码放入文档根目录下的.htaccess
:
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;
}
嗯,mod_rewrite无法查询您的数据库,因此它不能完全通过.htaccess本身完成。您可以在.htaccess中设置此规则:
通过httpd.conf
启用mod_rewrite和.htaccess,然后将此代码放入文档根目录下的.htaccess
:
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;
}
你确定你不想换个方向吗?对不起,是的,我想换个方向。我刚刚说对了:你敢肯定你不想换个方向吗?对不起,是的,我想换个方向。我有一个正确的:DGreat解决方案,但如果htaccess无法查询数据库,那么像pinterest这样的网站怎么会有这样的URL:(robingood是用户名,wow collection是类别)?@xRobot:即使如此,也有这样的URL方案。e、 g.此问题的URL为:http://stackoverflow.com/questions/10992072/is-there-a-way-to-rewrite-this-url/10996636
现在尝试为同一个问题输入此短URLhttp://stackoverflow.com/questions/10992072
您会注意到它被重定向到以前的URL。原因是它从数据库中提取问题标题,并将其附加到较短的URL中,以使其对SEO友好。在这种情况下,有问题的id(10992072),但在pinterest的情况下没有。这就是为什么SO有比pinterest更好的SEO URL,因为它将更快地查找id而不是长文本。无论是否有ID,这两个站点都使用一些服务器端代码从数据库中查找。(正如我在回答中所概述的)很好的解决方案,但是如果htaccess无法查询数据库,那么像pinterest这样的网站怎么可能有这样的URL:(robingood是用户名,wow collection是类别)?@xRobot:即使有这样的URL方案。e、 g.此问题的URL为:http://stackoverflow.com/questions/10992072/is-there-a-way-to-rewrite-this-url/10996636
现在尝试为同一个问题输入此短URLhttp://stackoverflow.com/questions/10992072
您会注意到它被重定向到以前的URL。原因是它从数据库中提取问题标题,并将其附加到较短的URL中,以使其对SEO友好。在这种情况下,有问题的id(10992072),但在pinterest的情况下没有。这就是为什么SO有比pinterest更好的SEO URL,因为它将更快地查找id而不是长文本。无论是否有ID,这两个站点都使用一些服务器端代码从数据库中查找。(正如我在回答中概述的那样)