How to remove controller name from the URL in CodeIgniter


In this tutorial I will explain how to remove controller name from the URL in CodeIgniter. In recent tutorial How to remove index.php from URL in CodeIgniter, we have explained how to remove index.php from the URL. Now we will remove controller name also from the URL.

For example, if our signup page URL is like http://localhost/ci/home/signup
here “home” is controller name and “signup” is controller function/method name.

Now if we remove controller name from URL, it should work, http://localhost/ci/signup.

Open application/config/routes.php file, and modify the code like below.

$route['404_override'] = '';
$default_controller = 'home';
$route['default_controller'] = "$default_controller";

// here I have removed controller name from URL
$controller_exceptions = array("signup"); // here signup is controller function name. You can add as many as controller function names.

foreach($controller_exceptions as $v) {
  $route[$v] = "$default_controller/".$v;
  $route[$v."/(.*)"] = "$default_controller/".$v.'/$1';
}

Now the signup page URL will work like: http://localhost/ci/signup.

Hope this tutorial will help you to remove controller name from the URL in CodeIgniter.