Fancy URLs for CodeIgniter (deleting index.php)

Fancy URLs for CodeIgniter (deleting index.php)

CodeIgniter is my favorite PHP framework for web development. It basically provides the tools you need while developing, and nothing more. Nothing fancy, just the basics. Since it doesn't force you to follow a particular path, you are absolutely free to select your own.

Today I am going to tell you a little trick. By default, the URLs in CodeIgniter are like:

http://domain.tld/index.php/controller/method/

(I assumed you put CI in your root web folder, not in a subfolder)

Since index.php is there for all URLs, it is OK to get it out of the way. Wait a second, I didn't mean deleting the file :P What we will do is telling the apache (now I did another assumption, you should be using apache as web server) "hey mate, this index.php dude will be called all the time, so let's not let the user see it in URL bar K?"

Enough chitchat, here is the procedure:

Open the file codeigniter_path/application/config/config.php

Edit the line

$config['index_page'] = 'index.php';

to

$config['index_page'] = '';

and the line

$config['uri_protocol'] = 'AUTO';

to

$config['uri_protocol']	= 'REQUEST_URI';

and finally, create a .htaccess file in your codeigniter_path and then add there lines:

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

That's all folks! Shorter URLs without index.php!