.htaccess重写规则问题

时间:2010-02-02 17:59:26

标签: .htaccess mod-rewrite

这是有效的:

RewriteRule ^([a-z]{2}/){0,1}showCategory/([0-9]*)/([0-9]*)(/{0,1})$                /main.php?id=$2&il[lang]=$1&page=$3 [L]

使用此网址:

http://localhost/showCategory/590/10

现在我也想用这个:

http://localhost/showCategory/590/transport/10

我试过的规则:

RewriteRule ^([a-z]{2}/){0,1}showCategory/([0-9]*)/([a-z\-_0-9\+]*)/([0-9]*)(/{0,1})$               /main.php?id=$2&il[lang]=$1&page=$3 [L]

如何更改RewriteRule

2 个答案:

答案 0 :(得分:1)

尝试此规则:

RewriteRule ^([a-z]{2}/)?showCategory/([0-9]+)/[a-z-_0-9+]+/([0-9]+)/?$ /main.php?id=$2&il[lang]=$1&page=$3 [L]

答案 1 :(得分:1)

这个适用于两个网址

RewriteRule ^([a-z]{2}/)?showCategory/([0-9]+)(?:/[A-Za-z0-9_-]+)?/?([0-9]+)/?$  /main.php?id=$2&il[lang]=$1&page=$3 [L]

我还用{0,1}简化了?这也是一样的。

密钥位于(?:/[A-Za-z0-9_-]+)?,解释:

(?:                   Non-capturing group, so what's in here will not be put in any $n
  /                   Match the slash
  [A-Za-z0-9_-]+      Match any word with letters/numbers/dashes/underscores
)?                    Close non-capturing group, and the ? means it's optional