CodeIgniter 3 REST Api cannot read routes

752 views Asked by At

I am doing a versioning in my REST API and have it structured this way

example.domain.com
↳ v1
↳ v2

It will be browsed like https://www.example.domain.com/v1 or https://www.example.domain.com/v2 where each version folder is the root of the REST API. Everything in v1 folder didn't have any issue with the routing but v2 cannot read the route other than $route['default_controller'] = 'welcome'; although the v2 is actually a copy of v1. I only changed the base_url under /application/config/config.php to follow the url I mentioned above.

Below code is the sample of what I have in /application/config/routes.php and have TestingController.php located in /application/controllers/api/

$route['test'] = 'api/TestingController';  // new route
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = TRUE;

This is what I have in TestingController.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
class TestingController extends REST_Controller {

    function __construct()
    {
        parent::__construct();
    }

    public function index_get(){
        echo 'testing';
    }

}

When I browse https://www.example.domain.com/v2/test it will give the error File not found. but https://www.example.domain.com/v1/test is working fine. I don't know where to look cause the codes are the same from both version folders.

I don't have any .htaccess file in my root folder of each version (could be the reason?) and if you want to check the REST_Controller it is here.

Also, as an additional information, I have removed the index.php from $config['index_page'] = ''; under /application/config/config.php, which is now an empty string.


EDIT:

I just tried adding .htaccess but also not working.

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

It appears to be working with index.php included https://www.example.domain.com/v2/index.php/test . That is very weird as v1 is working without that.

1

There are 1 answers

0
Lenin On

Check the httpd.conf file of your web server and change

#LoadModule rewrite_module modules/mod_rewrite.so

TO

LoadModule rewrite_module modules/mod_rewrite.so

Create a .htaccess file in your application root folder

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]