【问题标题】:Get id (category or business) from url after mod rewrite and assign into variable in phpmod重写后从url获取id(类别或业务)并分配给php中的变量
【发布时间】:2015-11-04 18:44:36
【问题描述】:

实际的 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 方法从?id= 2 中获取类别和产品的 ID。但是 get 方法在重写后将不起作用。如何获取 ID 也是页面类型(类别、产品)?

【问题讨论】:

  • 对于产品http://example.com/product-permalink/p/id 对于类别http://example.com/category-permalink/p/id

标签: php .htaccess mod-rewrite


【解决方案1】:

试试这个

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 表示如果规则匹配,则不再处理该规则以下的任何 RewriteRules。

【讨论】:

  • 所以我可以通过 GET 方法捕获字符串?
  • 但是我应该在上一页类别的链接参考上写什么?应该写成 category.php?id=2 还是 /c/53 ?如果是第二个,那么实际上它没有传递任何 id 对吗?
  • @TanjimaTani 使用/c/53
  • 好的。那么$_GET[''] 的索引应该是什么,因为 href 没有发送任何索引?
猜你喜欢
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 2023-01-08
相关资源
最近更新 更多