Forum Moderators: phranque

Message Too Old, No Replies

Rewriting for subcategories while building on category structure

         

jcannon

7:41 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



I have two possible scenarios where I need rewrite rules:

1. www.example.com/food/fruit/
2. www.example.com/food/fruit/apples/

I have the following rewrite rule for the 1st instance:

RewriteRule ([A-Za-z_-]+)/([A-Za-z_-]+) directory.php?one=$1&two=$2

And the following rewrite rule for the 2nd instance:

RewriteRule ([A-Za-z_-]+)/([A-Za-z_-]+)/([A-Za-z_-]+) directory.php?one=$1&two=$2&three=$3

When I enter the 1st URL, it works correctly, $1 = food and $2 = fruit.

However, when I enter the 2nd URL, $1 = php, $2 = fruit, and $3 = apples.

Any help would be appreciated. It would be nice if I could figure out a way to take care of all possible iterations with one line (ex: www.example.com/food/fruit/apples/red/golden-delicious)

Thanks!

[edited by: jdMorgan at 8:06 pm (utc) on Jan. 29, 2008]
[edit reason] example.com [/edit]

jdMorgan

8:12 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like some other rule is being invoked -- one that will accept "directory.php" as a match for its pattern. That's assuming that your patterns and filepaths are exactly as shown.

Just some general suggestions:

  • Put your rules in order from most-specific to least-specific (longest patterns first).
  • Make sure that all external redirects precede any internal rewrites.
  • Use the [L] flag on every rule, unless you know a very good reason not to.
  • Keep in mind that RewriteRules in an .htaccess context are effectively recursive: If the output of a rule matches the pattern of any other rule, then the output will be re-rewritten until no more rewriting is possible. Therefore loops must be explicitly prevented (usually by the use of a negative-match RewriteCond).

    [added]You also need to anchor your patterns properly using "^" and "$". [/added]

    Jim

    [edited by: jdMorgan at 8:14 pm (utc) on Jan. 29, 2008]

  •