Question

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.

Was it helpful?

Solution

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]

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top