开发者

mod_rewrite - convert paths to querystring

开发者 https://www.devze.com 2023-01-12 05:46 出处:网络
consider this rule RewriteRule ^/models/(.*)/(.*)$ /models?$1=$2 [NC,L] this rewrites /models/application/lawnmower to /models?appliction=lawnmower

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&开发者_StackOverflow中文版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


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.

0

精彩评论

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