As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a URL string. By default, it uses an index.php file in the CodeIgniter’ URLs, as shown below.

If you want to remove an index.php from a CodeIgniter URL and to generate a standard URL string to run the php file, you must follow these three steps, as shown below.

Step1. In this step, you need to create a .htaccess file in the root folder of CodeIgniter that is parallel to the application folder.

remove an index php file from the Codeigniter URLs

Step2. After creating a file in the application folder, write the following code in a .htaccess, but make sure that your Apache server has enabled mod_rewrite to accept file requests directly to the CodeIgniter URL without using index.php in the URL.

Step3. After successfully created a .htaccess file in the CodeIgniter application, now you have to make a small change in the application/config/config file. In this file, there is a $config[‘index_php’] variable that assigned a default ‘index.php’ value. Now remove an ‘index.php’ value from the $config variable and set it to blank as shown below.

Example: Create a MyController.php controller file and save it to the following path application/controller/MyController.php folder. And write the following program in the controller file.

MyController.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/MyController; it shows the output as shown below.

remove an index php file from the Codeigniter URLs

And in our previous topic, we have used index.php as a URL to execute a controller program in the localhost by invoking a URL localhost/Codeigniter-3.1.11/index.php/MyController, and then call a function, as shown in below image.

remove an index php file from the Codeigniter URLs

Pin It on Pinterest

Share This