Search Courses

Search Courses
Home / Blog
CodeIgniter : Remove index.php from URL

CodeIgniter : Remove index.php from URL

Posted on : 22,Dec 2014 1138

CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications. As much as I respect the framework and the help documentation along with it, what i have seen is some of the "help" does not work the intended way.

There are blogs in ellislab around how to remove index.php appearing in the URLs which does not work always. Let us have a look at slightly modified way, which is proven. Code below is well commented, if you are still facing issues, feel free to use comment section below.

Config file changes:- Go to “application/config/config.php”
Find below code:-
$config['index_page'] = 'index.php';

Replace with the below code:-
$config['index_page'] = '';

.htaccess changes :-  .htaccess file you can find in root , i.e where other folders like application, system etc resides. If you not have any .htaccess create  a new one and place it on your root. Add below code to .htaccess file
<ifmodule mod_rewrite.c="">
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Check if url call is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</ifmodule>
<ifmodule !mod_rewrite.c="">
# If we don't have mod_rewrite installed, all 404's page not found errors
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /index.php
</ifmodule>

If your codeigniter code is in a sub folder i.e. in WAMP www/mysite  then change this line in above snippet to

RewriteBase /mysite

Try your site pages with clean url i.e without index.php like http://example.com/controller/method.

If issue persists:- Need to apply 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';

Finally, it is good to have clean urls which are SEO friendly, changing URL won't impact your URI Routing



Join the discussion

Improve the thread with your input and questions.
Hari Ramesh
Big Data and Analytics Architect at Abbvie Inc

Member Since : Feb, 2014

LIBERTYVILLE Illinois

LinkedIn Twitter

CodeIgniter is a powerful open source PHP framework. Codeigniter is now supported by British Columb

Take course on


Interested in Codeigniter? We will notify you when knowledge is shared.