Last Updated: January 04, 2021
·
9.808K
· dtommasino

Remove index.php in CodeIgniter

By default the framework routes everything through the index.php file. You can change this default functionality by using mod_rewrite.

Adjust your config.php file in /application/config/config.php

Change:

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

To:

$config['index_page'] = ' ';

Next create a .htaccess file in the root folder using the following rewrite rules:

RewriteEngine on
RewriteCond $1 !^(index\.php|css|js|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Now instead of having to go to http://example.com/index.php you can just do http://example.com

Gotchas

If you are not using the root directory on your web server you will need to change the last rewrite rule to match your application root directory's path. For example if my app was location at:

/var/www/html/app1

I would have to include "app1" in the last rule like so:

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

If the rewrite rule isn't working, ensure you are allowing .htaccess files in your Apache config file. The directory section should contain an AllowOverride All option:

<Directory "/var/www/html/app1">
   Options Indexes FollowSymLinks
   AllowOverride All
   Order allow,deny
   Allow from all
</Directory>

Don't forget that any changes to the Apache config file require a service restart!

3 Responses
Add your response

IMO, this is hardly a pro tip. It's included in the documentation: http://ellislab.com/codeigniter/user-guide/general/urls.html

over 1 year ago ·

Until people learn to RTFM...this is still a hugely googled item.

over 1 year ago ·

@jakiestfu agreed, more of a beginner tip than anything. However, it's till a good pro tip just for the gotcha. This question is asked at least once or twice a week on the Ellislab forums. It's not the CI part that catches people out but the htaccess configuration for their particular setup.

over 1 year ago ·