开发者

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 a开发者_StackOverflow社区ll 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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜