Codeigniter 2, htaccess, and Friendly Urls

My htaccess file worked in Codeigniter 1.7 but everything seems broken in Codeigniter 2.0, here is how to fix it.

Making Codeigniter 2.0.0 Friendly

Late last month I learned of Codeigniter 2.0.0, and I was thrilled to see it moving in the for front.  So I have started a few new projects to use it, and I beat my head against the wall for about 2 hours reading the forums, and trying to figure out how to remove the "index.php" from the url to make friendly urls.  I used an htaccess file (identical to the one i used in Codeigniter 1.7) and I always ended up seeing the default controller no matter what I did.

When I tried to access my url like this:

http://localhost/controller/method/parameter

I was getting routed to my default controller, and when I tested, the URI helper was reporting that there were no SEGMENTS... But when I put the index.php back in it worked perfectly.

http://localhost/index.php/controller/method/parameter

At first I thought my server config was broken... but when I moved CI 1.7.3 in to the directory (instead of 2.0.0) it worked perfectly. So it wasn't the server, or the htaccess file.  After a lot of reading I found the answer. Here is how to get "friendly URLs" in Codeigniter 2.0.0, using htaccess. It is similar  to the instruction on Codeigniter's Wiki here... but I had to ADD an additional step to make it work for the new version... (All this is working on the assumption that your version of apache has htaccess enabled and configured properly.)

  1. Create your htaccess file
  2. Set $config[‘index_page’] to an empty string
  3. Set $config['uri_protocol'] = 'REQUEST_URI';

 

htaccess

Here is the content of the htaccess file (Taken directly from the Codeigniter Wiki here)

<IfModule mod_rewrite.c>
    
RewriteEngine On
    RewriteBase 
/

    
#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]
    
    
#When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    
RewriteCond %{REQUEST_URI} ^application.*
    
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
    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

You will need to modify this to reflect your root directory and server settings.

 

The Config

Next you need to modify your config file located at "/application/config/config.php" you will need to modify the following variables:

Change:

$config['index_page'] = 'index.php':

To:

$config['index_page'] = '';

 

Change

$config['uri_protocol'] = 'AUTO';

To:

$config['uri_protocol'] = 'REQUEST_URI';

For some reason (at least on my server) when the "uri_protocol" was set to AUTO, I could not get it to work properly.  Once I changed this value to REQUEST_URI... everything started working.

I hope this helps someone out, it cost me about 2 hrs.

Page Information:
  • Tags: Codeigniter 2, htaccess, friendly urls, index.php, broken, howto, routing
  • Description: How to remove the index.php from the url in Codeigniter 2.0.0.