0

I'm currently building an API and I want people to be able to make calls like this:

http://mydomain.com/api/method.name.json?apikey=1234

Which will load to the following:

http://mydomain.com/api/index.php?method=method.name&format=json&apikey=1234&field1=1&field2=2

I am fiddling with some Mod Rewrite code but cannot get it to work. And I'm note sure how to add query strings (apikey=1234&field1=1&field2=2) onto the end of the URL.

Here's what I have so far and it's not working. It's giving me a 404:

RewriteRule ^([a-zA-Z\.]+).(json|jsonp|xml|php)+)$ index.php?method=$1&format=$2 [L]

Is what I am trying to achieve possible? Thanks in advance!

Ben Sinclair
  • 3,896
  • 7
  • 54
  • 94
  • Look at the answer given [here](http://stackoverflow.com/a/7738433/1967396) - or better yet, the original documentation [here](http://httpd.apache.org/docs/2.4/logs.html). It shows you how to turn on the debug log - this will tell you exactly how requests are being handled. Once you know how your expression is being interpreted, it will make writing the correct expression a breeze. – Floris Aug 13 '13 at 17:24

2 Answers2

0
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$
Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
0

Worked it out! The dot in the following example:

([a-zA-Z\.]+)

Was causing index.php to be returned as the $1 variable. To overcome this I simply added:

RewriteCond $1 !^index\.php$

To exlude index.php. And replacing [L] with [QSA,L] adds the query string to the end.

So my final code was:

RewriteCond $1 !^index\.php$
RewriteRule ^([a-zA-Z\.]+)\.(json|jsonp|xml|php)$ index.php?method=$1&output=$2 [QSA,L]
Ben Sinclair
  • 3,896
  • 7
  • 54
  • 94