Ross McFarlane
Ross McFarlane

Reputation: 4254

mod_rewrite for PHP RESTful Server

I'm developing a PHP RESTful server for an API locally on my mac.

I've managed to enable mod_rewrite and have Overrides allowed for the site directory (~/Sites/api).

In ~/Sites/api is the .htaccess file and index.php. I'd like to rewrite all requests to http://localhost/~myusername/api/* to index.php. I need to preserve the query parameters, but that's it.

I've tried the following in the .htaccess file:

Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) index.php [QSA,NC,L]

This gives a 500:Internal Server Error.

Commenting out the FollowSymLinks line gives a 403:Forbidden error.

I can access index.php fine without the rewrites in place.

Any help you could offer would be much appreciated. I feel like weeping at the moment.

Thanks, Ross

Upvotes: 3

Views: 4924

Answers (2)

Kornel
Kornel

Reputation: 100110

The problem is that [L] means stop rewriting this URL, but you're generating a new URL with index.php, which will be rewritten as well, causing infinite loop.

Add RewriteCond that excludes index.php from being rewritten.

Upvotes: 1

Andrew Sledge
Andrew Sledge

Reputation: 10351

RewriteRule ^api/(.*)$ index.php?handler=$1 [L,QSA]

Where the handler is what is passed to your index.php script. For instance, a request for

http://localhost/~myusername/api/getUser/myusername

would be rewritten to

http://localhost/~myusername/api/index.php?handler=getUser/myusername

Upvotes: 7

Related Questions