Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Removing index.php from your codeigniter 2 url with htaccess

David Carr

Tutorials PHP & MySQL

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 Your Logo Your Logo Your Logo

Become a sponsor

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

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.