Reputation: 2767
Setup
I have my file structure set up as so:
/root
|
/api
|
/Slim PHP framework
index.php
|
index.php
The index.php
inside the Slim directory contains the routes required to retrieve the JSON
data from a Mongo
database. E.g.
$app->get('/users(/:id)', function($id = null) use ($app, $collection) {
/* Do mongo search and echo json_encoded data back */
});
I have an .htaccess
file contains: which removes the .php
extension from files under the root.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php`
Question
I can access my JSON
data using url: http://localhost:8888/root/api/index.php/users
.
However, what I would like to do is access the data using the url: http://localhost:8888/root/api/users
Upvotes: 1
Views: 1601
Reputation: 693
One thing of note, the sequence of rules is also important, so if you need to rewrite/redirect http://example.com/users AND http://example.com/users/ to http://example.com/userinfo/ while at the same time rewriting http://example.com/users/uname to http://example.com/scripts/script.php?id=uname then this one should work: RewriteEngine on
RewriteEngine on
RewriteRule ^users/$ /userinfo/ [L]
RewriteRule ^users/(.*)$ /scripts/script.php?id=$1 [L]
RewriteRule ^users$ /userinfo/ [L]
There could be a better solution with one line less but this one works for me.
Upvotes: 0
Reputation: 24448
If I understand correctly, what about this?
RewriteRule ^api/users/(.*)$ /root/api/index.php/users/$1 [L]
RewriteRule ^api/users$ /root/api/index.php/users [L]
That should allow this URL.
http://localhost:8888/api/users/1
and it will allow this too
http://localhost:8888/api/users
EDIT:
That should allow you to add a number after users. Also shortened the URL so you don't have to include root in the URL as well.
Upvotes: 3
Reputation: 51711
Assuming, your .htacces is located in website root "/"
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_URI} !/index.php [NC]
RewriteRule ^(.*?/api)/(.*)$ $1/index.php/$2 [NC,L]
This would redirect
http://localhost/root/api/users/1 => http://localhost/root/api/index.php/users/1
http://localhost/root/api/data/20 => http://localhost/root/api/index.php/data/20
Upvotes: 1