开发者

.htaccess rewrite, how do I do it for this scenario?

I would like to have开发者_C百科

eg.

www.domain.com/api/json/implode/abc/and/a/b/c...

equal to

www.domain.com/api/jason/?method=implode&key=abc&param1=and&param2=a&param3=b&param4=c...


You could do something like this:

RewriteRule ^/api/json/([/]*)/([/]*)$ /api/jason/method=$1&key=$2
RewriteRule ^/api/json/([/]*)/([/]*)/([/]*)$ /api/jason/method=$1&key=$2&param1=$3
RewriteRule ^/api/json/([/]*)/([/]*)/([/]*)/([/]*)$ /api/jason/method=$1&key=$2&param1=$3&param2=$4
RewriteRule ^/api/json/([/]*)/([/]*)/([/]*)/([/]*)/([/]*)$ /api/jason/method=$1&key=$2&param1=$3&param2=$4&param3=$5
RewriteRule ^/api/json/([/]*)/([/]*)/([/]*)/([/]*)/([/]*)/([/]*)$ /api/jason/method=$1&key=$2&param1=$3&param2=$4&param3=$5&param4=$6
# keep expanding this pattern out for however many paramas you need to be
# able to handle...

But cleaner would be something like this:

RewriteRule ^/api/json/([/]*)/([/]*)$ /api/jason/method=$1&key=$2
RewriteRule ^/api/json/([/]*)/([/]*)/(.*)$ /api/jason/method=$1&key=$2&params=$3

ie: stuff all trailing optional params into a single parameter.


I don't think it's possible to dynamically figure out the number of params, but you can do something close:

RewriteRule ^/api/json/([^/]+)/([^/]+)/(.*) /api/jason/?method=$1&key=$2&params=$3

This will extract method and key individually but put all the additional parameters into params, which you can split later within your script.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜