How to remove index.php from URL in CodeIgniter



If we need a clean URL in CodeIgniter. We need to remove index.php from url in CodeIgniter. So in this tutorial I will show you, how to remove index.php from URL in CodeIgniter.

In default you will get index.php in url in CodeIgniter. like :-

http://codeigniter313.com/index.php/controller/function

Codeigniter provide easy way for url rewrite functionality to get clean url or remove index.php from url in codeigniter. So we will follow some steps below to remove index.php from url in codeigniter to get clean, user friendly or seo friendly url.

Steps to remove index.php from url in codeigniter:

1. Go to “application/config/config.php

Find below code:-

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

Replace with the below code:-

$config['index_page'] = '';

2. If you have not any htaccess file create a new and put it on your root folder. Now include following code in the htaccess file.

# Turn on URL rewriting
RewriteEngine on
RewriteBase /

# Protect application and system files from being viewed
RewriteRule ^(core|modules|system) - [F,L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-f 

# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1

if you have codeigniter in subdirectory change RewriteBase / to RewriteBase /subdir/.

Now open your site pages you will found clean url with no index.php. So your all url looks like http://codeigniter313.com/controller/function. Now any of HTTP request for index.php will be treated as a request for your index.php file.

If you still found any issue, you need to change one more config changes.

Go to “application/config/config.php

Find below code:-

$config['uri_protocol'] = 'AUTO'; 

Replace with the below code:-

$config['uri_protocol'] = 'REQUEST_URI';

I hope these steps will help you to remove index.php from url in codeigniter and get clean url in codeigniter.