4

I am writing a REST api in codeigniter using https://github.com/chriskacerguis/codeigniter-restserver REST controller library. I have written my controller inside application/controllers/api/v1 and override the Router class to allow multiple sub-folders inside controllers.

Now I want to access the services as http://api.domain.com/v1/user/11 instead of http://api.domain.com/api/v1/user/11

I have tried following htaccess rule

RewriteCond %{HTTP_HOST} ^api\.domain\.com$
RewriteCond %{REQUEST_URI} !index.php/
RewriteRule ^(.*)/?$ /index.php/api/$1 [QSA,L]

But it seems to be not working because REQUEST_URI in the $_SERVER global variable is set to v1/user/11 instead of api/v1/user/11 so codeigniter is unable to find the controller.

Is there a way to achieve the requirement except setting the altering REQUEST_URI in index.php?

I do NOT want to do permanent redirect.

Any help is much appreciated.

1 Answer 1

3

A route rule as below should help:

$route['v1/(:any)/(:any)'] = 'api/v1/$1/$2';

If v1 is supposed to change like v1, v2 in future, then something similar to below will help:

$route['v(:num)/(:any)/(:any)'] = 'api/v$1/$2/$3';
Sign up to request clarification or add additional context in comments.

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.