How to remove index.php from codeigniter URLs in two simple steps!

Published

If you have downloaded codeigniter, and learning it from scratch, you might have been at a point where you search "How to remove index.php from codeigniter URLs"



Generally any function inside your controller will work only if you call it after index.php in your URL.

for example,
localhost/wamp/codeigniter-project/ will load the welcome page

and if you add a method/function inside the basic controller, eg:test, you will have to reach it by

localhost/wamp/codeigniter-project/index.php/test/

Now you want to achieve the URL like localhost/wamp/codeigniter-project/test/

Lets see how to make it possible to remove index.php from your codeigniter project.

first of all, i have edited controller, welcome.php

current content of welcome.php is below

<?php

* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('welcome_message');
}

public function test()
{
echo "yay.. this is test controller";
}


}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
?>


it will work only if i call like this.

localhost/wamp/codeigniter-project/index.php/test/

but if i remove index.php fromurl, it just wont work.

So lets solve this with two steps.


This is the folder anf files view inside basic codeigniter bundle


-application
-system
-user_guide
-.htaccess = < edit this file
-.index.php
-license.txt


1st step:

Open above .htaccess in your favorite text editor
and add below lines into that .htaccess file to remove index.php from your codeigniter project.

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

Save it.

2nd step:

open cofig.php file inside /application/config/ folder.

search for the line "$config['index_page']".

Change $config['index_page'] to blank in config.php in application folder as below:

$config['index_page'] = '';

now, visit your page without index.php

eg: localhost/wamp/codeigniter-project/test/

if you could see functions content, this works for you.

If not, check rewrite module is on in Apache modules.

Comments

Post a Comment