CodeIgniter: Removing index.php

CodeIgniter (CI) is one of the MVC frameworks available. Unlike legacy PHP technique, This framework use different URI format for sending and receive parameters. For example: if we use usual PHP technique, we will use URI like:

localhost/index.php?id=2

But in CI, the URI would be look like this:

localhost/index.php/welcome/2

Where index.php is the router, welcome is the controller, and 2 is the parameter given to the welcome controller. It’s because of its design pattern, the MVC (Model-View Controller).

After some reads, there were a lot of discussion on removing the “index.php” in CI forum. And I’ve been spending my time just to figure it out. I found some good resources at CodeIgniter Wikis, and after some trial-and-error, finally it works for me. This is how I do it:

  1. I created .htaccess file and put it on my CI installation folder. If you used Windows, you could use Save As… menu from Notepad to create this file.
    My CI installation folder was www/igniter, so I put the .htaccess file on this folder. And this is my .htaccess file:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /igniter
    
    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #‘system’ can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]
    
    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn’t true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    #This last condition enables access to the images and css folders, and the robots.txt file
    #Submitted by Michael Radlmaier (mradlmaier)
    RewriteCond $1 !^(index\.php|images|robots\.txt|css)
    RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>
    
    <IfModule !mod_rewrite.c>
    # If we don’t have mod_rewrite installed, all 404’s
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin
    
    ErrorDocument 404 /index.php
    </IfModule>

    I just copy-paste the code from CI’s Wiki and modified the RewriteBase into my CI installation folder (in my case: igniter)

  2. Then, I opened the system/application/config/config.php file. I modified the $config['index_page'] from
    $config['index_page'] = "index.php";

    into

    $config['index_page'] = '';
  3. I activated mod_rewrite on the Apache. I opened the httpd.conf file and delete the # character in front of:
    LoadModule rewrite_module modules/mod_rewrite.so
  4. I didn’t need to modified the directory section of httpd.conf file. Maybe it had been already configured by the Apache.

And it works for me. The URI now without the index.php segment. I implemented all of the configuration above on Windows using Wamp. I tried it on Linux too, but so far It didn’t work. I’ll tell you later if I make it.

23 thoughts on “CodeIgniter: Removing index.php

  1. Dude! Thanks a bunch! This worked finally!

    I tried unsuccessfully to remove the 'index.php' from my urls on my CodeIgniter project and this is the only version of the .htaccess file that worked for me! Thank you again!

  2. wah Code Igniter memang mantap, aku juga lagi belajar CI. pokoknya sip deh. materi yang cukup bagus, mas arif aku tunggu artikel berikutnya tentang CI makasih.

  3. Pingback: Copdir
  4. Pingback: KR350
  5. This code is not working for me sir. i tried searching for the problem where ever i could. Just that i’m not getting it right on my system When i do this and try to open in the browser, i just get the list of directories in the CI root folder. Please help me out of this.

What's in your mind?