Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite question...

Please help a mod_rewrite new - B - Thank you!

         

Brian07002

7:41 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Hi There,

My site is made up of dynamic pages (excluding my homepage). I have categories & subcategories of pages pointing to product pages. What I am trying to accomplish is being able to access both my category / subcategory & product pages in a search engine friendly url.

I have been to this place:

http://www.webmaster-toolkit.com/mod_rewrite-rewriterule-generator.shtml

That place will generate my rewrite rules, which works fine when I put ONE url into my .htaccess file, but if I go ahead and add 2 or more rules they don't work...I am sure I am doing something wrong on my part, but I can't figure it out...

Let me show you exactly what I have tried in my .htaccess that worked. Note: It works without anything else.

Options +FollowSymLinks
RewriteEngine on
RewriteRule index/(.*)/(.*)/(.*)/(.*)/$ /shop/index.php?$1=$2&$3=$4

The above would be to convert my CATEGORY links to search engine friendly urls, which works fine if it were by itself, but how would I add the next 2 rules included with the one above?

Options +FollowSymLinks
RewriteEngine on
RewriteRule index/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ /shop/index.php?$1=$2&$3=$4&$5=$6

Options +FollowSymLinks
RewriteEngine on
RewriteRule index/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ /shop/index.php?$1=$2&$3=$4&$5=$6&$7=$8&$9=$10

Thank You in Advance!

-Brian

[edited by: jdMorgan at 8:35 pm (utc) on Dec. 6, 2004]
[edit reason] No URLs, please - See TOS [/edit]

jdMorgan

9:04 pm on Dec 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Brain,

The problem you're seeing is due to the use of the pattern ".*" in your rules, and the fact that you've got the short ones first. Since ".*" will match anything, and match as much as possible, *all* of your URLs will be matched by the first, and shortest, rule. The first ".*" will end up matching --and including in the back-referenc "$1" -- as many parameters as possible without leaving any of those that follow empty. This kind of match is horribly inefficient too, because the matching routine will iterate through several times, once for each parameter, and end up "backfilling" the requested URL into the match groups.


Requested URL: a/b/c/d/
Pattern: ^(.*)/(.*)/(.*)/$
First pass: $1="a/b/c/d", $2="", $3="", NO MATCH - $2 empty, $3 empty, can't match on last two slashes
Second pass: $1="a/b/c", $2="d", $3="", NO MATCH - $3 empty, can't match on last slash
Third pass: $1="a/b", $2="c", $3="d", MATCH

Obviously, this gets much worse as the number of parameters increases.

So, two points of "style" here:

First, Put your longest rule first if you cannot avoid using ".*"

Second, instead of using ".*", use "[^/]+". This will allow a forward-looking negative match, and the whole pattern can be evaluated left-to-right in one pass.


RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /shop/index.php?$1=$2&$3=$4&$5=$6&$7=$8&$9=$10 [L]
RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /shop/index.php?$1=$2&$3=$4&$5=$6&$7=$8 [L]
RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /shop/index.php?$1=$2&$3=$4&$5=$6 [L]
RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /shop/index.php?$1=$2&$3=$4 [L]
RewriteRule ^index/([^/]+)/([^/]+)/$ /shop/index.php?$1=$2 [L]

Also, use the [L] flag to terminate rewriting if a match is found.

See the references cited in our charter [webmasterworld.com] for more information.

Jim