One of the most common problem that a beginner faces with codeigniter is the presence of index.php in the URL just before the name of the controller. This makes the URL look odd. Also, whenever developers are trying to form a link during the development of a project, it becomes mandatory for the developer to add index.php in the URL otherwise the application does not work and keeps on providing a 404 Error (Not FOund).
So, in order to make the URL of the codeigniter user friendly , we can use .htaccess file. This .htaccess file helps us in changing the default routing behavior of Codeigniter. This .htaccess file contains some line of code which can help us achieve our target and it is placed in the root directory of codeigniter framework i.e. along with the application folder (NOT inside the application folder).
Here is a sample block of code that we can place in the .htaccess file :
RewriteEngine On
RewriteBase /builder
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index.php|images|robots.txt|css|docs|js|system)
RewriteRule ^(.*)$ /builder/index.php?/$1 [L]
Please note the last line in the block of code. There is index.php written over there. We are also adding something just before the index.php in the last line of code. This additional thing is nothing but the name of the folder which holds the codeigniter application. We are also placing name of the folder in the second line.
Doing only and only this we can not fully change the routing behaviour and resolve our problem related to the presence of index.php in the URL. After placing the code in the .htaccess file and then placing the .htaccess file in the root directory of codeigniter. We now need to make some changes to the config.php file. The config.php file is located in the application/config folder. When you open config.php file you will find $config array in the file. This $config array holds various configuration parameters required to run codeigniter application. One of these parameters is index_page. It will look something like this $config['index_page'] and it will be the next parameter after base_url. By default , it will contain the value index.php. We need to make it blank i.e. $config['index_page'] = '';.
Now, our work is complete and we can now run the codeigniter application without the presence of index.php in the URL.