Please take a look at what’s in my codeigniter config/routes.php file:
$route['default_controller'] = "primary";
$route['404_override'] = '';
$route['(:any)'] = 'primary';
$route['login'] = 'login';
$route['admin'] = "admin";
$route['admin/(:any)'] = 'admin/$1';
So, I want pretty much any page request to be directed to my ‘primary’ controller, where it will be taken care of by the index() function there. There are a few exceptions; login goes to login, admin to admin. That’s all working fine. The problem lies in the last rule, which doesn’t seem to work as it should; ‘admin/whatever’ just gets routed back to my primary controller. Why?
I would actually like to route any admin requests normally, so admin/whatever/ goes to admin/whatever/, and admin/whatever/whatever/ goes to admin/whatever/whatever/. There could be many segments, and there are too many to list individual rules (needed as exceptions to the first $route[‘(:any)’] = ‘primary’; rule). Is there a way to do this?
I’m fairly new to codeigniter, so apologies if answers are glaringly obvious. I’ve spent the last few hours digging around for answers though, and can’t find any.
I was being stupid. The rules should be ordered by order of priority, I had a catchall route (:any) before the login and admin routes, so the route always went to the primary controller and never reached my other routes since that was matched first.
Routes should also be ordered by number of segments, so ‘admin/(:any)’ should come before ‘admin’. In the rule ‘admin/(:any)’, the (:any) it seems can refer to any number of subsequent segments.
I now have:
and it works fine.