Removing index.php from your codeigniter 2 url with htaccess

David Carr

1 min read - 8th May, 2012

In Codeigniter by default all calls to your controller from a http request will have index.php inside the url such as http://www.yourdomain.com/index.php/blog it would look much better to be http://www.yourdomain.com/blog don't you agree?

Luckily if your using a Linux host you can use mod-rewrite to mask all http requests to index.php removing the need to have index.php as part of the url.

All you need to do is put the following in a .htaccess file in the same directory as your Codeigniter installation typically the route of the server if its not adjust the path as required.

RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

In a nutshell this turns the rewrite engine on, checks the requested url is not index.php or images or robots.txt or a file or directory then it will be routed to index.php and pass any subsequent segments in the form for $1.

This results in links without index.php will not work, you will still need to update any manual links yourself as the link with index.php will still be valid but are not optional.

0 comments
Add a comment

Copyright © 2024 DC Blog - All rights reserved.