views:

20

answers:

2

Hi there,

I would like to have

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...

cheers

James

+1  A: 

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.

Laurence Gonsalves
Hi thanks for your answer, I know having more than 10 parameters is silly, but I would like to know if there is a programmatic way to do this?
James Lin
@James Lin: If you have so many unnamed parameters, why do you need them to be separate parameters in the first place?
casablanca
@casablanca because from the URL wise looks cleanereg. www.domain.com/api/json/add/1/2 www.domain.com/api/json/multiply/1/2/3
James Lin
@James Lin: Your URL would still look the same, you would just pass the last bunch of parameters directly to your script (eg. 1/2/3) and then split them up there.
casablanca
+1  A: 

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.

casablanca