How to remove index.php in codeigniter?

Last updated 5 years, 6 months ago | 2627 views 75     5

Tags:- CodeIgniter

Codeigniter | Remove index.php from URL in Codeigniter?

The file index.php is the root file of Codeigniter. it contains setting code for CodeIgniter. So, when we redirect to other pages the URL comes with index.php which looks ugly and also not good for SEO points of view. we can simply say that the URL is not user friendly.

The URL looks like this:

http://localhost/codeigniter/index.php/welcome/demo

OR

http://www.example.com/index.php/welcome/demo

Now for removing the index.php from the URL, we need to do some simple step:

Step1: Open the config.php file located in the "/application/config" folder. Find and modify the below code.

//  Find the below codes

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

$config['uri_protocol'] = "AUTO"


//  replace with these codes

$config['index_page'] = ""

$config['uri_protocol'] = "REQUEST_URI" 

Step2: Create a .htaccess file in the project root directory. The .htaccess file is used to modify or change the configuration of the apache server. The below code tells the apache server to ignore index.php in requested URI. Copy the below code in .htaccess file.

It should be located as:

your_project_name folder > .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]