开发者

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 li开发者_JAVA百科ke 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.


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]


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜