Search code examples
apacheurl.htaccessmod-rewriteslug

Complex htaccess rewrite rules - where do i start?


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.


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]