views:

135

answers:

2

Hi

I have asked on here before about mod_rewrite and got the perfect answer but now I need to do something more complex and really needed the advice of someone who knows mod_rewrite much better.

My rewrite rule looks like this so far:

RewriteRule ([a-zA-Z_-]+)/([0-9]?) index.php?cat=$1&page=$2

And that shows the URL as follows: /categoryname and shows pages like /categoryname/1/.

But I would really like to modify the rule to work with sub categories such as /categoryname/subcategoryname/ and still work with the page system (subcat/2/) but I just can't seem to wrap my head around this.

It needs to ignore the sub cat rewrite if you are on the main category etc.

Hoping someone can help me.

+1  A: 
RewriteRule [a-zA-Z_-]+/([a-zA-Z_-]+)/([0-9]?) index.php?cat=$1&page=$2 [L]
RewriteRule ([a-zA-Z_-]+)/([0-9]?) index.php?cat=$1&page=$2 [L]
chaos
that is good but i don't really want to use a cat and subcat variable, is it possible to feed the subcat name to the cat variable but if no subcat is present use the first cat ?
David
Sure. Editing...
chaos
oh wow, works like a dream, can't thank you enough :)
David
just curious if i wanted a third cat would i just add /([a-zA-Z_-]+)/([0-9]?) to the end ?
David
Well, more like you'd add another `[a-zA-Z_-]+/` in front. Same pattern, in any event.
chaos
hi, just to clarify would i add the third one at the beginning of the statement (just after Rule) or further along as i can't seem to get it work...
David
chaos
A: 

I would use these rules:

RewriteRule ^([a-zA-Z_-]+)/([a-zA-Z_-]+)/(([1-9][0-9]*)/)?$ index.php?cat=$1&subcat=$2page=$4
RewriteRule ^([a-zA-Z_-]+)/(([1-9][0-9]*)/)?$ index.php?cat=$1&page=$3

With the assertion for the start (^) and the end of the string ($) there is no ambiguity to have one of those rule accidentally applied. And the (([1-9][0-9]*)/)? part is for the optional paging.

Gumbo