CodeIgniter | Remove index.php from URL using htaccess

On 12/17/2014

Hi, we’ll see how to remove index.php from url in codeigniter. Even though codeigniter uses search engine friendly urls, nobody would have missed the awkward presence of 'index.php' in between those codeigniter's urls. Have you ever wondered how to get rid of it to get more clean urls in your codeigniter application? Say your app has an url something like http://www.example.com/index.php/contact and you want it to look like http://www.example.com/contact. Yes, you can do it. In codeigniter removing index.php from url should be done by rewriting urls in htaccess file.

How to Remove index.php from URL in CodeIgniter using .htaccess?

Recommended Read: How to Create Login Form in CodeIgniter, MySQL and Twitter Bootstrap

Recommended Read: CodeIgniter/MySQL Tutorial - Read and Display data with Twitter Bootstrap

Step 1: Enable Mod Rewrite Option in your APACHE Server

To rewrite urls in the htaccess file, the mod_rewrite option should be enabled in the apache server. Goto apache's "httpd.conf" file and search for the line,

LoadModule rewrite_module modules/mod_rewrite.so

If the above line is preceded with # (in which case, the module is disabled), then remove the # (hash) symbol to enable url rewriting.

Step 2: Create '.htaccess' File

Next create the .htaccess file at the root directory. To create the htaccess file, open your favourite text editor and create a new text file. Save it as ".htaccess" (make sure you type the filename within quotes to avoid the file to be saved as text file) in the root directory.

htaccess-file-codeigniter

Now copy paste this code to the htaccess file and save it.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

If you run your codeigniter app as a subdirectory instead of root domain like, http://example.com/cisite/ instead of http://example.com/ then the above code won't work. You have to tweak it little to tell that your site runs as a subdirectory. Add this code to the htaccess file instead of the above one.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Step 3: Modify CodeIgniter Config Settings

Open config.php file under application >> config folder and search for the line,

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

Remove index.php from it to look like this,

$config['index_page'] = '';

Now restart apache and check the url. eg., http://www.example.com/contact instead of http://www.example.com/index.php/contact

Recommended Read: How to Upload Files in PHP Securely

Recommended Read: How to Insert JSON into MySQL using PHP

If it doesn't work, you may be having problem with uri protocol. In that case find $config['uri_protocol'] ='AUTO' setting in config file and replace with $config['uri_protocol'] = 'REQUEST_URI'.

Restart apache server again and you can check the codeigniter site to see that index.php has been removed from codeigniter url.

Last Modified: Jul-1-2016

28 comments:

  1. finally found the solution at this page.
    thank you so much. Great blog.
    Sem Results Portal RTU eSuvidha

    ReplyDelete
  2. When i removed the index.php in the url it redirects me to localhost/xampp.

    ReplyDelete
    Replies
    1. Hey! if you are working in localhost then use the line,

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

      without '/' before 'index.php' like this,

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

      Now restart apache and check the url, say like 'http://localhost/cidemo/dashboard' instead of 'http://localhost/cidemo/index.php/dashboard'

      Hope this helps :)

      Delete
    2. I got the same problem, I remove that slash before index.php but the same it goes to localhost/wampserver.

      Delete
    3. the Same Problem having with me...when i try to remove the index.php in the url it redirects me to localhost/xampp....

      Delete
  3. Thank you...

    ReplyDelete
  4. The above code did not work for me. This one did

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

    ReplyDelete
  5. Above code not work for me... what i do?

    ReplyDelete
  6. I follow your procedure but it goes to localhost/wampserver. Whats wrong with.Plz help me

    ReplyDelete
  7. I am using MAMP and tried this and several other methods and it doesn't work. there might be something wrong in the httpd.conf. Has any1 figured this out yet?

    ReplyDelete
    Replies
    1. Try this one
      RewriteEngine On
      RewriteCond $1 !^(index\\.php|resources|robots\\.txt)
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php/$1 [L,QSA]

      Delete
    2. Try this:
      RewriteEngine On
      RewriteCond $1 !^(index\\.php|resources|robots\\.txt)
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php/$1 [L,QSA]

      Delete
  8. I am using MAMP and tried this and several other methods and it doesn't work. there might be something wrong in the httpd.conf. Has any1 figured this out yet?

    ReplyDelete
  9. my url goes like this http://127.0.0.1/mysite/mycontroller

    and it says the requested URL /mysite/mycontroller was not found on this server.

    ReplyDelete
  10. Object not found!

    The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

    If you think this is a server error, please contact the webmaster.

    Error 404

    localhost
    Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.20

    ReplyDelete
  11. I followed your instructions for creating a Htaccess file and copied the code, but I don’t know what I should be doing after following this step. Can you help?

    ReplyDelete
  12. Hello is this the right setup for CodeIgniter 3.1.x in xampp?

    Can you kindly confirm? Maybe it could be great to update the article pointing this out.

    Also for the CI tutorials me (and probably every body else) will unzip the CI package as subdir in the htaccess so this could be the default supposition

    Last one question
    Early steps
    - CI has been unzipped in a subdir "citut" of htdocs
    - I open the config.php
    - I just and ONLY edit the base url
    - I start xampp
    - I point the browser to localhost/citut
    - it correctly shows the welcome default

    Some kind explanation from an expert is welcome on diving a little in the routes.php and in the htaccess

    e.g. is it the htaccess a must?
    What we can do without htaccess? Or
    What we can do with just routes.php?

    Some forms will call some controllers that won't call/show any view.

    Where to place those controllers?
    How to handle their routing?

    Thank you and thank you really for your tutorials, they are between the really few that make sense and allows the reader to achieve some results (this is the reason for asking you some of the above updates)

    Thank you

    ReplyDelete
  13. Hello Valli, two users here says that the htaccess modification in conjunction with removing the index.php from the config.php is making the behavior that opening the url of the ci app, will show this url http://localhost/dashboard/ which is the home of the xampp installation, the same page that shows opening http://localhost/ in a new xampp installation or either portable.

    I confirm this issue because I have the same issue right now.

    Codeigniter is 3.1.4 while xampp is the latest 5.6.x portable

    We come here from your other tutorial
    http://www.kodingmadesimple.com/2015/05/install-setup-php-codeigniter-framework-xampp-localhost.html

    and so we get stuck :-|

    Maybe something is changed in the Codeigniter routing? Thank you for testing you too.

    ReplyDelete

  14. RewriteEngine On
    # RewriteBase /
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]


    this is the modification that made local xampp working

    ReplyDelete

Contact Form

Name

Email *

Message *