Rewrite rule to simplify codeigniter code for REST api

I am writing a REST api in codeigniter using https://github.com/chriskacerguis/codeigniter-restserver REST controller library. I wrote my controller inside application/controllers/api/v1

and overrides the Router class to allow multiple sub-folders inside controllers.

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

I have tried following the htaccess rule

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

      

But it doesn't seem to work because the REQUEST_URI in the $ _SERVER global variable has a value v1/user/11

instead api/v1/user/11

, so codeigniter can't find the controller.

Is there a way to achieve this requirement other than setting the REQUEST_URI change in index.php?

I do NOT want to do a permanent redirect.

Any help is greatly appreciated.

+3


source to share


1 answer


A route rule like below should help:

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

      



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

$route['v(:num)/(:any)/(:any)'] = 'api/v$1/$2/$3';

      

+2


source







All Articles