How to remove "index.php" in codeigniter's path

125,866

Solution 1

If you are using Apache place a .htaccess file in your root web directory containing the following:

RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Another good version is located here:

http://snipplr.com/view/5966/codeigniter-htaccess/

Solution 2

I had some big issues with removing the index.php. As a general rule the .htaccess below has been tested on several servers and generally works:

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

<Files "index.php">
AcceptPathInfo On
</Files>  

If you don't have any luck with that then the next step is to adjust your config file. Try some of the other URI protocols e.g.

| 'AUTO'            Default - auto detects
| 'PATH_INFO'       Uses the PATH_INFO
| 'QUERY_STRING'    Uses the QUERY_STRING
| 'REQUEST_URI'     Uses the REQUEST_URI
| 'ORIG_PATH_INFO'  Uses the ORIG_PATH_INFO

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

If your still not having any luck try changing the rewrite rule to include your subfolder. This is often a problem if your using a temporary URL on a dev server etc:

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

Just play around with these options, one should work. Also, make sure your index file is set to:

$config['index_page'] = '';

Good luck!

Solution 3

Have the.htaccess file in the application root directory, along with the index.php file. (Check if the htaccess extension is correct , Bz htaccess.txt did not work for me.)

And Add the following rules to .htaccess file,

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

Then find the following line in your application/config/config.php file

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

Set the variable empty as below.

$config['index_page'] = '';

That's it, it worked for me.

If it doesn't work further try to replace following variable with these parameters ('AUTO', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_URI', and 'ORIG_PATH_INFO') one by one

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

Solution 4

Have a look in the system\application\config\config.php file, there is a variable named index_page

It should look like this

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

change it to

$config['index_page'] = "";

Then as mentioned you also need to add a rewrite rule to the .htaccess file

Note: in CodeIgniter v2 this file was moved out of the system folder to application\config\config.php

Solution 5

All above methods failed for me and then I found that I was not changing AllowOverride None to AllowOverride All in my virtual host file at /etc/apache2/sites-available/default

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
            Options FollowSymLinks
            AllowOverride All    <---- replace None with All
    </Directory>
    <Directory /var/www >
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All   <---  replace None with All
            Order allow,deny
            allow from all
    </Directory>

     ...

Share:
125,866

Related videos on Youtube

OrangeRind
Author by

OrangeRind

What about me? Nothing really. Regular guy. Bit of brain. Lots of interests.

Updated on July 08, 2022

Comments

  • OrangeRind
    OrangeRind almost 2 years

    How do I remove the "index.php" sticking out in every path in codeigniter somewhere in the center? I want clean non index.php-fied URLs?

  • OrangeRind
    OrangeRind over 14 years
    +1 for the link! Although had to tweak around a bit. got it with this --> RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [QSA,L]
  • Willy
    Willy almost 12 years
    Thank you so much, i got that AllowOverride None too, now it works !
  • Kevin Beal
    Kevin Beal over 11 years
    The blog is gone now, and I would very much have liked to read it. None of these solutions are working for me...
  • Ahmed Samy
    Ahmed Samy over 11 years
    thank you , i have been failing in this for long time dunno why but ur way worked like butter :D
  • mohsin.mr
    mohsin.mr over 11 years
    After spending couple of hours, I read this and found my mistake.
  • sanji
    sanji about 11 years
    Thanks MDeSilva. This post save my time.
  • Jasdeep Khalsa
    Jasdeep Khalsa almost 11 years
    Another module I needed to enable was actions: stackoverflow.com/questions/14419757/…
  • jsky
    jsky almost 11 years
    i thought your first code here was the solution for me but actually it is just sending any url like domain/* to domain/index.php it ism't sending domain/welcome to the welcome controller. was excited for a second then. trying the other suggestions now.
  • Henry
    Henry over 10 years
    for some reason, i had to add a slash to the beginning of the RewriteCond. RewriteCond $1 !^\/(index\.php|assets|robots\.txt)
  • Stony
    Stony over 10 years
    Thanks. It's works. BTW, I needn't change config.php.
  • CMCDragonkai
    CMCDragonkai over 10 years
    For some reason, if you have index.php in the URL, it's not actually removed, it stays there and keeps working.
  • T.Coutlakis
    T.Coutlakis over 10 years
    Thanks, this one helped me, I used PATH_INFO with WAMP
  • vadim
    vadim over 10 years
    I also spent at least an hour because of this "little" trick. By default, the .htaccess files comes in application directory instead of root.
  • JChow
    JChow almost 10 years
    awesome, saved my day.
  • Roshdy
    Roshdy over 9 years
    I used all the possible combinations and it's still not working for me!!!!! how did you get it to work??
  • Sean Vieira
    Sean Vieira over 9 years
    Your Apache install needs to be configured to check .htaccess files - make sure that is the case.
  • Someone
    Someone about 9 years
    Tried using the first of the above examples works only on my root but not on form loads etc will still try to use index.php in urls I will play around and come back to this if I can firugre it out
  • Benjamin W.
    Benjamin W. about 8 years
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
  • Dewald Els
    Dewald Els over 7 years
    This is correct and straight from the CodeIgniter docs. I also using this method.
  • gomesh munda
    gomesh munda over 7 years
    Thanks @Vinod.I was going crazy with this problem.I have been searching on the net for so long, tried various solutions but they never worked until i came across your solution.Now my CI project is working and it has given me NEW HOPE to move on with CI.Thanks a lot :)