URL重写php查询字符串


URL rewrite php query strings

我有两个url做一些查询,我正在努力以我想要的方式重写。

首先

/shop/index.php?category=catslug

我想要的

/shop/category

第二

/shop/index.php?product=slug

/shop/category/product

我有这个当前:

RewriteRule ^shop/([A-Za-z0-9-]+)/?$ shop/index.php?category=$1 [L]
RewriteRule ^shop/[A-Za-z-]+/([A-Za-z0-9-]+)/?$ shop/index.php?product=$1 [NC,L]

问题是其中一条规则破坏了所有以shop开头的东西,所以像shop/cart这样的东西不起作用。我很困惑。这可能吗?

注意,对于shop/cart, cart部分匹配第一个RewriteRule[A-Za-z0-9-]+部分。因此,它被重写为shop/index.php?category=cart

避免这种情况的方法,希望你有相对较少数量的固定url,是在你的两个规则之前有一个RewriteRule,如
RewriteRule ^shop/(cart|this|that|other|thing) - [L]
RewriteRule ^shop/([A-Za-z0-9-]+)/?$ shop/index.php?category=$1 [L]
RewriteRule ^shop/[A-Za-z-]+/([A-Za-z0-9-]+)/?$ shop/index.php?product=$1 [NC,L]

对于匹配管道分隔字符串之一的请求(如cart), -表示不更改请求,[L]表示最后(不继续下一个规则)。