修改后从url获取id(类别或业务),并在php中分配到变量中

修改后从url获取id(类别或业务),并在php中分配到变量中,php,.htaccess,mod-rewrite,Php,.htaccess,Mod Rewrite,实际的URL类似于- /category.php?id=2和/product.php?id=56。我在htaccess文件中应用了以下规则- RewriteEngine On RewriteRule ^.+/p/([0-9]+) product.php?id=$1 [NC,L] RewriteRule ^.+/c/([0-9]+) category.php?id=$1 [NC,L] 我得到了以下url模式- /c/2 and `/p/56` 到目前为止还不错 以

实际的URL类似于-
/category.php?id=2
/product.php?id=56
。我在htaccess文件中应用了以下规则-

RewriteEngine On
RewriteRule   ^.+/p/([0-9]+)   product.php?id=$1    [NC,L]
RewriteRule   ^.+/c/([0-9]+)   category.php?id=$1    [NC,L]
我得到了以下url模式-

/c/2 and `/p/56` 
到目前为止还不错 以前,我可以通过get方法从like
?ID=2
获取类别和产品的ID。但get方法在重写后将不起作用。如何获取ID以及页面类型(类别、产品)

试试这个

RewriteEngine On
RewriteRule   ^.+/p/([0-9]+)   product.php?id=$1    [QSA,L]
RewriteRule   ^.+/c/([0-9]+)   category.php?id=$1    [QSA,L]
QSA意味着,如果有一个查询字符串与原始URL一起传递,它将被追加到重写中


L表示如果规则匹配,则不处理此规则下的任何重写规则。

因此,使用此方法,我可以通过GET方法捕获字符串?但我应该在上一页类别的链接引用上写什么?它应该写为
category.php?id=2
还是
/c/53
?如果第二个,那么它实际上没有传递任何id,对吗?@TanjimaTani use
/c/53
确定。那么
$\u GET['']
的索引应该是什么,因为href没有发送任何索引?对于产品
http://example.com/product-permalink/p/id
用于类别
http://example.com/category-permalink/p/id