×

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 Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

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

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 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 File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 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 Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

8 minutes read.

CodeIgniter URI Class

URI Class The CodeIgniter contains a URI class that is used to retrieve information from URI strings. It also provides suitability to get information from the re-routed segments. The system automatically initializes this...

6 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 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 Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

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

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

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

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 Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes 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 Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.