-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
  • 34,719
  • 24
  • 185
  • 251
jskidd3
  • 4,188
  • 14
  • 59
  • 116

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
  • 39,856
  • 8
  • 91
  • 133
1
RewriteEngine on
RewriteRule /?v1/names/(.+)/(.+) /v1/names?gender=$1&age=$2 [L]
chanchal118
  • 3,339
  • 2
  • 25
  • 50
  • 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