views:

37

answers:

2

This is working:

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

with this URL:

http://localhost/showCategory/590/10

Now I want to work with this too:

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

The rule I tried:

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]

How to change the RewriteRule?

+1  A: 

Try this rule:

RewriteRule ^([a-z]{2}/)?showCategory/([0-9]+)/[a-z-_0-9+]+/([0-9]+)/?$ /main.php?id=$2&il[lang]=$1&page=$3 [L]
Gumbo
Thanks, it's OK!How does it work?
Viktor Onozo
@Viktor Onozo: How does what work?
Gumbo
+1  A: 

This one works with both URLs

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

I also simplified the {0,1} with ? which does the same.

The key is in (?:/[A-Za-z0-9_-]+)?, explanation:

(?:                   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
Infinity
Thanks, it's OK! How does it work?
Viktor Onozo
I updated the answer with an explanation
Infinity
Viktor Onozo
Viktor Onozo
If .../0/1 works but .../0/2 doesn't, it's probably because you're not using the right parameter.If you're using PHP, in your php script do `print_r($_GET)` and see if you're using the right param
Infinity
Viktor Onozo