Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to set up a hierarchy five levels deep with mod_rewrite. This is what I have put together at this point. It works for the last two levels (the first and second rule) but not after that. Any help would be well appreciated.

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /mr/index.php?product_group=$1&product_family=$2&product_category=$3&product_sub_category=$4&product=$5 [C]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /mr/index.php?product_group=$1&product_family=$2&product_category=$3&product_sub_category=$4 [C] 
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /mr/index.php?product_group=$1&product_family=$2&product_category=$3 [C]
RewriteRule ^([^/]*)/([^/]*)$ /mr/index.php?product_group=$1&product_family=$2 [C] 
RewriteRule ^([^/]*)$ /mr/index.php?product_group=$1 [L]

I am not a server tech. I am a developer and just can't figure this out. Each of these rules works fine by itself. I need the rule(s) to work from one level to five and every step in between. I need a drill down style navigation that is SEO friendly.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

It looks like you've chained all the rules together with the [C] flag. See the documentation for an explanation of their meanings. You probably want [L] for "last" instead.

It's also worth noting that the rules will be run until the URL does not change any longer; so your second-to-last rule will likely create a rewrite loop, unless you have a rule before it similar to this:

RewriteRule ^/mr/index.php$ - [L]

Edit: For clarity, this is what I'd suggest as a starting point. You may get some insight from the logging provided by mod_rewrite.

RewriteRule ^mr/index.php$ - [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /mr/index.php?product_group=$1&product_family=$2&product_category=$3&product_sub_category=$4&product=$5 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$         /mr/index.php?product_group=$1&product_family=$2&product_category=$3&product_sub_category=$4 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$                 /mr/index.php?product_group=$1&product_family=$2&product_category=$3 [L]
RewriteRule ^([^/]*)/([^/]*)/?$                         /mr/index.php?product_group=$1&product_family=$2 [L]
RewriteRule ^([^/]*)/?$                                 /mr/index.php?product_group=$1 [L]
share|improve this answer
    
Fredden, Thanks but I do have a [L] on the last rule. I've tried a hundred combinations and am still not there. I'm not a server person but do need to get this working for a new project. –  zzz Nov 14 '13 at 10:51
    
I was trying to say that you probably don't want the rules chained at all, but that each rule should stand alone. As it stands, only the first rule will ever be processed properly, the others will be skipped if the first doesn't match. –  fredden Nov 17 '13 at 1:51
    
Fredden, That seems to work well - thanks! I still don't fully understand mod_rewrite and will try to fit in more research on it soon. There are some anomalies but nothing I can't overcome on the code side. See: [link] (ninog.com/mr) –  zzz Nov 17 '13 at 16:18
    
Given those examples, I'd add /? before the $ on each of the rules (except the first). That means "optionally with a trailing slash." Also, it looks like I missed a [C] when I copied your code - change that for an [L]. So you'd end up with something like: RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /mr/inde... I'll edit my post. –  fredden Nov 17 '13 at 20:37
1  
That's perfect! Man I really appreciate your help. –  zzz Nov 18 '13 at 14:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.