Tutorials, PHP & MySQL

Removing index.php from your codeigniter 2 url with htaccess

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.

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.