December 1, 2011 CodeIgniter Image

Removing index.php from URL in Codeigniter

Removing index.php from URL in Codeigniter

This simple post was a request from a follower.

As people start using codeigniter for their projects, they see that in the URL there is an extra “index.php” that just looks kinda ugly, and in some cases, unprofessional. Thank goodness there’s a mod rewrite available that can get rid of this index.php out of the address and make your urls look from this:

http://yourdomain.com/index.php/controller/method/etc..

to this:

http://yourdomain.com/controller/method/etc…

Now that second one looks a lot nicer, especially with the names you give your methods, or functions, whatever you want to call them. I had that index.php in one of my projects once, and just never wanted to take it out, just because I thought it was a tidous task. Now that I have done it, I recommend it to everyone that uses codeigniter.

There are 3 solutions for removing the index.php from the URL. Since I have only used one of them, that’s the only one I want to write about, and it’s extremely easy to do. It has worked for me so far in all projects I have done, including my localhost, and third party web hosting service.

Solution

Go to your root folder: (this is where your codeigniter folder is). Within the root folder, create a new file, withtout no name, but with the extension of .htaccess

Once you have this file created, open the file copy the following lines into it.

Once you have copied the above code, save the file and close. That’s it! Now go to your browser, and go into your website. You should now be able to access your site without having to type in: index.php

9 thoughts on “Removing index.php from URL in Codeigniter

  1. Excellent article sir; in an aditional note, some hosting providers will require a little twitch in this line:

    RewriteRule ^(.*)$ /index.php?/$1 [L]

    by removing the slash

    RewriteRule ^(.*)$ /index.php?$1 [L]

    Sometimes it’s also required to make some minor modifications to our application/config.php file; here are the changes I needed to make in order to force GoDaddy to remove the index.php:

    $config[‘index_page’] = ‘index.php?’;
    $config[‘uri_protocol’] = ‘QUERY_STRING’;

    Hope it helps!

  2. Removing index.php issue on codeigniter
    Hi, i face Removing index.php issue on CI 2.0…I need solutions for removing index.php on CI base URL..
    I used .htaccess file
    RewriteEngine on
    RewriteBase /mydomain.com
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|css|images|js|robots\.txt)
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    but above code cant remove index.php on base_url .. When I run in local host base_url removed index.php and working properly, but when I hosted the project in web, default controller page only working.
    1. My code:
    :application/config/routes.php
    $route[‘default_controller’] = “home”;
    :application/config/config.php
    /* Base Site URL */
    $config[‘base_url’]= ‘www.mydomain.com/’;
    /*| Index File*/
    $config[‘index_page’] = ‘ ‘;
    /* URI PROTOCOL */
    $config[‘uri_protocol’] = ‘REQUEST_URI’;
    : .htaccess.txt
    RewriteEngine on
    RewriteBase /mydomain.com
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|css|images|js|robots\.txt)
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    Result url: [ http://www.mydomain.com/search/ ]- page not found
    2. Now I used alternate code:
    : application/config/config.php
    /* Base Site URL */
    $config[‘base_url’]= ‘www.mydomain.com/index.php’;
    Result url: [ http://www.mydomain.com/index.php/search/%5D – page working properly
    How can I removing index.php in BASE_URL

  3. Gracias amigo, me tenia algo loco este tema de eliminar el index.php, pero gracias a estas sentencia porfin pude hacerlo.

  4. can you explain how to write .htaccess file step by step and also explain all the things. i write this but i cant understand how works it……thanks in advance……..and one more things some exter things which i see here i cant see onother tutorials…thanks for this….

Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.