3

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

2 Answers 2

7
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

Sign up to request clarification or add additional context in comments.

4 Comments

You should exclude index.php.
@Gumbo: please provide explanation with your statement if you don't mind.:)
@kaziTanvirAhsan That comment is more than three years old. I have no idea what it was supposed to mean.
@kaziTanvirAhsan Ok, now I know what it supposed to mean: ^api/(.*)$ will also match api/index.php. Therefore index.php should be excluded to be matched by this rule, e. g., with an additional RewriteCond $1 !=index.php.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.