Search code examples
php.htaccesscodeigniterhttp-redirectcodeigniter-routing

How to remove the controller name from url in codeigniter, and still be able to redirect on another controller?


I'm building an website using Codeigniter and I've changed the way the URL looks from http://localhost/site_controller/home to http://localhost/home.

I've changed that by using .htaccess -> RewriteBase changing the $config['index_page'] to $config['index_page'] = '';
and in routes I've change like that:

$route['default_controller'] = "site";

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

And it works fine only for the site controller, BUT my problem is that I have another controller named admin and when I try to login and make a redirect using redirect('admin/index') I get a 404 error.

How can I redirect from site controller to admin controller?


Solution

  • If you are going to route from (:any) => site you have to specify a higher priority route for the admin controller that overrides that route for those pages.

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