codeigniter

How to remove controller name from URL in CodeIgniter

In CodeIgniter or any MVC application, the regular URL structure is like example.com/controller/function/parameter which appear as example.com/user/profile/ariful.  But sometimes we may want to omit the controller name here to make the url more beautiful like example.com/profile/ariful or even example.com/ariful as best url.

This beautiful url can be achieve through the URI Routing of CodIgniter. In
For example.com/ariful, use this code

 application/config/route.php

application/config/route.php file, you’ve to write following code for example.com/profile/ariful

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

For example.com/ariful, use this code

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

If you have multiple parameters like

public function profile($username, $city)
{
     echo "You are ".$username." from ".$city;
}

Your route will be

5
  $route['(:any)/(:any)']                  = "user/profile/$1/$2";

If you want to use number in parameter instead of string like these examples, you need to use (:num) instead of (:any).

If you are looking for something like modifying URL, I hope this post may help you. If you’ve any further question, you can comment bellow.

Published by

Ariful Haque

Software Engineer | Freelance Web Application Developer PHP, CodeIgniter, WordPress, JavaScript, jQuery, Ajax, Ubuntu, CentOS, Apache..

2 thoughts on “How to remove controller name from URL in CodeIgniter”

Leave a Reply

Your email address will not be published. Required fields are marked *