English CodeIgniter: Removing index.php

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.

Related posts:

  1. Programming Hadoop in Netbeans
  2. Crunchbang Statler 10: First Look
  3. Hadoop on Single Node Cluster
  4. Running Hadoop Cluster in Netbeans
  5. Blogroll on a Page
  • RC

    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!

  • http://arifn.web.id sidudun

    sure…

    i'm glad it helps you..

  • Pingback: 500 Roundup: the Statistics | sidudun

  • http://www.yalamber.com yalamber

    That helped me out.

  • http://pujianto.net Pujianto

    Nice trick :).

    siap untuk dicoba :

  • Pingback: CodeIgniter: Removing index.php | out of the wheel

  • http://wahjusaputro.blog.ugm.ac.id wahjusaputro

    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.

  • Pingback: Removing ‘index.php’ from CodeIgniter URL | I Made Krisna's Times

  • Arif Raza

    :sakit: I try this method you mention but this does not word it is giving 500 error msg.

  • http://earli.us Earli

    :hi: keren gan, nubie ijin nyimak tutornya

  • Daniel

    Thank you very much. It really helped me!

  • sivanthi

    its working for me very very thanks sir

  • Rajashekar

    thanks very much

  • Pingback: Copdir

About Me

It's me, Arif N. In this blog I'll write about my adventure related to computer, programming, and anything that I found interesting. I wish you a happy reading.. :D

Related Posts

  1. Programming Hadoop in Netbeans
  2. Crunchbang Statler 10: First Look
  3. Hadoop on Single Node Cluster
  4. Running Hadoop Cluster in Netbeans
  5. Blogroll on a Page