How to remove index.php from Codeigniter URLs

Posted on May 26, 2013 in Web Dev

Codeigniter is an awesome PHP framework that makes PHP development much simpler, and much much faster. I’ve only been getting to grips with it for a few days, but have noticed there are some quirks that come packaged that you’re going to want to get rid of. One of these is the fact that ‘index.php’ is present in all URLs as standard.

It’s not difficult to fix this issue.

Step 1

Go to /application/config/config.php/ and change the following line:

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

to

$config['index_page'] = '';

As in the screenshot below (I’m using Sublime Text 2 in case anyone is wondering):

codeigniter-indexphp

 

Step 2

Now make sure you have a .htaccess file in the Codeigniter root. Note that as standard, the .htaccess file comes in the /application/ directory… You need to move the .htaccess file up a directory for it to work.

Step 3

Add the following to the .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

 

Leave a comment

Was this helpful? Did I miss something? Do you have a question? Get in touch, or tell me below.