×

How to remove an index.php file from the Codeigniter URLs?

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.

localhost/codeigniter3.1.1/index.php/My_controller/

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.

RewriteEngine On
 RewriteBase /codeIgniter-3.1.11/  // write the root folder name
 RewriteCond %{REQUEST_URI} ^system.*
 RewriteRule ^(.*)$ /index.php/$1 [L]
 RewriteCond %{REQUEST_FILENAME} !-f  // it defines the file name
 RewriteCond %{REQUEST_FILENAME} !-d // it accepts all the directory 
 RewriteRule ^(.*)$ index.php/$1 [L] 

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.

$config[‘index_page’] = ‘index.php’; // before setting it to blank
 $config[‘index_page’] = ‘’; // after set to blank. 

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

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class MyController extends CI_Controller
 {
     public function index()
     {   
   echo "<title>Tutorial and Example </title>"; 
      echo "<h3> It is an Example of remove an index.php file from the CodeIgniter URL </h3>";
     }
     public function test()
     {   
         echo "<title>Tutorial and Example </title>";
         echo "<h4> Welcome to the Tutorial and Example </h4>";
     }
 }
 ?> 

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

Related Topics

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

4 minutes read.

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 minutes read.

How to remove an index.php file from the Codeigniter URLs?

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...

2 minutes read.

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

6 minutes read.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 minutes read.

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

5 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

3 minutes read.

CodeIgniter Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

5 minutes read.