Codeigniter Remove index.php With .htaccess And URI Routing Tutorial

Codeigniter tutorial
Why we need to define custom routes? After moving with our codeigniter tutorials we are yet not able to create a beautiful SEO friendly URL structure. In production environment it is very important to have a SEO friendly and structured URL for your controllers and views, this impacts your website traffic in a dramatic way. So in this tutorial we will learn about:
  • Removing index.php from codeigniter URL with htaccess.
  • Setting custom routes with codeigniter routes.php
As we are moving with our new tutorials, we in previous tutorials we studied about many things in codeigniter, if you want to refresh what is being studied in previous tutorial you can always take a look. Let’s begin.

Codeigniter removing index.php With .htaccess and routing tutorial

Check Your htaccess File

Removing index.php from codeigniter URL with .htaccess file:

  What is a .htaccess file? Htaccess is a configuration file used in apache environment to let the apache server know what added functionalities are needed. With a .htaccess file one can turn on and off custom functionalities in an apache server environment. When a .htaccess file is detected by apache then it processes it first then applies the rules of .htaccess to the website. How .htaccess files are created? It is very easy to create a .htaccess file, just create a new text document save it with .htaccess extension and name of the file should be blank. Htaccess files are always placed in the root directory of your web application. Before applying .htaccess rules make sure mode rewrite is enabled on your server, else enable it.

How to remove index.php from codeigniter URL using htaccess

Here is the simplest codeigniter htaccess file that can help you remove index.php from your URL and make it look a bit more beautiful.
<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]

  Sample .htaccess file is attached in this tutorial. Download that if you need some real example. The above .htaccess rules will rewrite your URL and remove the index.php from your URL structure. Check your routes configured properly

URI Routing With Codeigniter

In codeigniter there is one to one relation between the URL and the corresponding class or method. Whenever we point to a URL of codeigniter application you are pointing to a function of a controller inside your codeigniter application, and might you are passing some parameters along with the function and controller. The basic URI segments in codeigniter is Your_codeigniter_root_url/your_controller_name/your_function_name/your_parameters_in_sequence_seperated_with_backslashes Example: We can remove this default URI structure and revamp it as per our needs, in some cases we may need a different name for a different controller or function. URI routing allows you to let select your own reference string and create relation with any controller/method/function and when requested with the string used, it loads the appropriate function or method or controller. See the examples below:
Codeigniter URI Our Custom URL
This is how we can create our own custom URLs and codeigniter will refer to the actual class/methods. Now let us do some real routing. configuring routes in codeigniter

Configuring routes in codeigniter

Open your codeigniter installation directory then application->config and then open routes.php in your text editor. Scroll down to bottom of the document, now remember in the previous chapters we had created our Home controller? We will set the index function of home controller as home page for our application.

Defining default controller

Default controller is the controller which is loaded every time base URL is loaded, in routes.php you can see a $route['default_controller'] variable set to load the welcome controller by default, change it to the controller you want to be loaded by default. i.e. I will change it to home controller, so it will load our home controller every time we visit the base URL of our application.
$route['default_controller'] = 'home';
Now once done save route.php, open browser and go to root URL of your codeigniter application. If you have configured it properly, you will able to see your new home page.   Overriding default 404 in codeigniter You can create your own custom 404 page in codeigniter, just you need to set the 404_override value, for setting up your custom 404 page in codeigniter, select the controller you want to use as your 404 controller and define it in routes.php as shown below.
$route['404_override']= 'Your_404_controller/404_function';
  Setting custom routes in codeigniter Now we will configure custom route for our contact_us, about_us and privacy_policy functions inside the home controller. For routing the syntax is: $route[‘Your-string’]=’controller/function/arguments/’ So add the routes in your routes.php
$route['about-us'] = 'home/about_us';

$route['privacy-policy'] = 'home/privacy_policy';

  • The first configured route says codeigniter to load the home controller and about_us method of home controller when is browsed.
  • Second route says codeigniter to load the home controller and privacy_policy method of home controller when is browsed.
  • The third route says codeigniter to load the index method of contact_us controller when is browsed
We will learn advance routing in in future advance tutorials. The sample .htaccess and routes.php files are attached below, you can download and check them. In next tutorial we will learn doing CRUD with codeigniter. routes and htaccess tutorial sample If you have any suggestions in mind or you have anything to share or we missed something in this tutorial in your view. Please let us know please leave your comments below. Also please share if you find this tutorial helpful.


  1. Hey would you mind letting me know which web host you’re utilizing?
    I’ve loaded your blog in 3 different internet browsers and I must say this blog loads a lot quicker
    then most. Can you recommend a good hosting provider at a fair price?
    Kudos, I appreciate it!


Please enter your comment!
Please enter your name here