Codeigniter Routes Controller Sub Folder

15,570

Put the admin route before the any route:

$route['admin/(:any)'] = "admin/$1";
$route['(:any)'] = "site/$1";

otherwise it will always hit any and redirect to site. You have to give it a chance to match admin before matching any.

Share:
15,570
PhilWeb
Author by

PhilWeb

I have a degree in Computer Engineering and Electronics at the University of Perugia. I work like web developer and mainly use PHP, HTML/HTML5, CSS/CSS3 and Javascript. I recently studied these framworks: Code Igniter Bootstrap 960 Grid Jquery To improve my skills. At the university I learn Java and I study Tomcat, Jetty and Apache Solr.

Updated on June 05, 2022

Comments

  • PhilWeb
    PhilWeb almost 2 years

    I've a problem with routes and controller. I've got 2 types of controller: first type is used to manage the webpages, second type is used for cms and I prefer to put them in a sub-folder. Example:

    /controller/site.php (for webpages)
    /controller/admin/ (for controllers to manage cms)
    

    in routes.php I've write:

    $route['(:any)'] = "site/$1";
    $route['admin/(:any)'] = "admin/$1";
    

    I've got the file .htacces set in this way:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|robots\.txt|css|js|font|woff|ttf|svg|eot|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    

    and this variable on config.php:

    $config['index'] = '';
    

    but it works only for "site". If I write "mywebsite/admin/login" for example, it return 404 error.

    I've found also MY_Router to extend CI_Route but doesn't work.

    Can someone help me to resolve this problem ?

  • PhilWeb
    PhilWeb over 10 years
    THANKS A LOT! I've solved the problem! I didn't think that changing the order would have create so many troubles. Thanks again!