Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

Removing index.php from your codeigniter 2 url with htaccess

David Carr

PHP & MySQL Tutorials

    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.

    Laravel Modules Book by David Carr

    Help support the blog so that I can continue creating new content!

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

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