views:

7

answers:

1

consider this rule

RewriteRule ^/models/(.*)/(.*)$ /models?$1=$2 [NC,L]

this rewrites /models/application/lawnmower to /models?appliction=lawnmower

nice, just what i want

now consider this rule (there are up to 6 name/value pairs)

RewriteRule ^/models/(.*)/(.*)/(.*)/(.*)$ /models?$1=$2&$3=$4 [NC,L]  

this should rewrite

/models/application/lawnmower/series/xp

to

/models?application=lawnmower&series=xp  

but it actually creates

/models?application/lawnmower=series&GX=  

ultimatly, what I need to do is take every value that follows /models and parse it into name/value pairs in qs form

any thoughts

A: 

With a bit more information, it likely is possible to take your URLs with an arbitrary number of key-value pairs and convert them to a query string to pass to your script. If I remember correctly, Gumbo answered a similar question with a sort of "loop" that would allow for something like that.

However, as I believe he pointed out at the time, and I'll point out to you now, doing so with mod_rewrite is really not a good idea. It is much better to just have mod_rewrite rewrite your URL to your script:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/models/.*$ /models [NC,L]

...Then parse the original REQUEST_URI value to extract the information that you want. Your scripting language of choice is a much more suitable tool for this task than mod_rewrite is.

As far as mending your current rule, I think perhaps your request to the server had a trailing slash? Since your (.*) groupings can match anything (or nothing), they'll greedily consume the first slash as part of the key name if you have three other slashes following it. The simple solution would be to not match slashes in your capture group:

RewriteRule ^/models/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /models?$1=$2&$3=$4 [NC,L]  

Note that if you're trying to do this as a series of six separate rules, you're going to run out of backreferences, since the most you can get (that you define) here is 9.

Tim Stone
Thanks Tim.Im not sure I understand this comment"However, as I believe he pointed out at the time, and I'll point out to you now, doing so with mod_rewrite is really not a good idea. It is much better to just have mod_rewrite rewrite your URL to your script:"Aside from that, for whatever reason, there was a request for a .js file that was referenced using document relative references and that was causing the mangled rewrite (changing that script ref to root relative corrected the rewrite)
jason
@jason - I was just pointing out that if your URL becomes too complex, it's often a better idea in terms of efficiency and simplicity to let your scripts do the work of parsing the data out of the URL, instead of having `mod_rewrite` do it. In this case you would use `mod_rewrite` to change the URL to point to your script, then you use your script to determine what the key-value pairs should have been.
Tim Stone
@jason - Ah ha, nice catch on the JavaScript file.
Tim Stone
ahhh... makes sense
jason