-1

Current API usage: http://api.mydomain.com/v1/names?gender={gender}&age={age}

What I want usage to be: http://api.mydomain.com/v1/names/{gender}/{age}

Can I use .htaccess to rewrite this URL?

Cœur
  • 37,241
  • 25
  • 195
  • 267
jskidd3
  • 4,609
  • 15
  • 63
  • 127

2 Answers2

3

Assuming both parameters are numeric:

 RewriteEngine on
 RewriteRule /v1/names/([0-9]+)/([0-9]+) /v1/names?gender=$1&age=$2 [L]

If they can also contain alphanumeric characters replace both instances of [0-9]+ for example with [A-Za-z0-9]+ or whatever fits the requirement.

Apart from that I'd recommend doing this in PHP for more flexibility.

Community
  • 1
  • 1
Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
1
RewriteEngine on
RewriteRule /?v1/names/(.+)/(.+) /v1/names?gender=$1&age=$2 [L]
chanchal118
  • 3,551
  • 2
  • 26
  • 52
  • The `QSA` is only needed if it is allowed and accepted to pass extra GET-parameters. I'd recommend against it if these 2 are definitely the only parameters to reduce possible attack vectors. – Niels Keurentjes Dec 22 '13 at 12:26