PHP & MySQL | Tutorials

David Carr

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 it would look much better to be 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.

Domains are often purchased from multiple providers, keeping track of where a domain is and its DNS settings can be tricky. Domain Mapper solves this by listing all your domains in one place. View your DNS settings and receive reminders to renew your domains. Try it today.

Support my work by donating with PayPal.

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2009 - 2021 DC Blog. All code MIT license. All rights reserved.