116

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
  • 4,618
  • 12
  • 42
  • 56

27 Answers27

90

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/

Sean Vieira
  • 140,251
  • 31
  • 286
  • 277
  • 2
    +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] – OrangeRind Sep 18 '09 at 16:47
  • for some reason, i had to add a slash to the beginning of the RewriteCond. `RewriteCond $1 !^\/(index\.php|assets|robots\.txt)` – Henry Sep 08 '13 at 18:21
  • For some reason, if you have index.php in the URL, it's not actually removed, it stays there and keeps working. – CMCDragonkai Dec 03 '13 at 08:06
  • 1
    I used all the possible combinations and it's still not working for me!!!!! how did you get it to work?? – Roshdy Jan 10 '15 at 15:06
  • Your Apache install needs to be configured to check .htaccess files - make sure that is the case. – Sean Vieira Jan 10 '15 at 18:05
60

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!

jimbo2087
  • 1,034
  • 8
  • 19
  • 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. – jsky Aug 14 '13 at 11:37
31

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';
Shaolin
  • 2,451
  • 4
  • 27
  • 37
21

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

Re0sless
  • 10,022
  • 5
  • 47
  • 64
9

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>

     ...

waqas
  • 3,907
  • 3
  • 31
  • 40
6
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /codeigniter/index.php/$0 [PT,L]

after changing RewriteRule .* index.php/$0 [PT,L] with the project folder name "codeigniter".its working for me. Thanks guys for your support.

Captain Sparrow
  • 854
  • 14
  • 23
6

Step 1 => Place your .htaccess file in root folder where application and system folders exist.

Step 2 => If your web path using sub-folder like - yourdomain.com/project/ - then use following code in htaccess file

RewriteEngine on
RewriteBase    /project/
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /project/index.php/$1 [L]

If your web path using root-folder like - yourdomain.com - then use following code in htaccess file

RewriteEngine on
RewriteBase    /
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Community
  • 1
  • 1
vinod
  • 2,342
  • 1
  • 16
  • 22
  • 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 – Sam Mar 19 '15 at 12:04
  • 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 :) – gomesh munda Feb 01 '17 at 05:34
5
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 

This must definitely work.. have a try

Ramaraju.d
  • 1,227
  • 5
  • 25
  • 41
5

Just thought i might add

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

would be the .htaccess and be sure to edit your application/config.php variable in the following manner:

replace

$config['uri_protocol'] = “AUTO” 

with

$config['uri_protocol'] = “REQUEST_URI”
Jefkine Kafunah
  • 130
  • 1
  • 5
4

Ensure you have enabled mod_rewrite (I hadn't).
To enable:

sudo a2enmod rewrite  

Also, replace AllowOverride None by AllowOverride All

sudo gedit /etc/apache2/sites-available/default  

Finaly...

sudo /etc/init.d/apache2 restart  

My .htaccess is

RewriteEngine on  
RewriteCond $1 !^(index\.php|[assets/css/js/img]|robots\.txt)  
RewriteRule ^(.*)$ index.php/$1 [L]  
Jules
  • 13,397
  • 12
  • 50
  • 92
miguelitomp
  • 1,113
  • 1
  • 8
  • 7
  • Another module I needed to enable was `actions`: http://stackoverflow.com/questions/14419757/how-to-get-htaccess-files-working-with-apache-virtualhost/14481297#14481297 – Jasdeep Khalsa Jun 22 '13 at 13:34
4

Perfect solution [ tested on localhost as well as on real domain ]

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 
Abhishek Goel
  • 15,517
  • 8
  • 81
  • 62
3

Use mod_rewrite as instructed in this tutorial from the CI wiki.

Ben S
  • 65,441
  • 30
  • 165
  • 210
3

This is an .htaccess for one of my CI projects:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /projectFolder/index.php/$1 [L] 

The last line should give you what you need, though you may or may not need '/projectFolder' depending on how your folder structure is set up. Good luck!

Matthew Rapati
  • 5,488
  • 4
  • 25
  • 48
3

As an .htaccess file, a good option is to use the one provided by the Kohana Framework:

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /

# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
    Deny From All
</Files>

# Protect application and system files from being viewed
RewriteRule ^(?:application|system)\b.* index.php/$0 [L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

It's a well thought out .htaccess that just works.

laurent
  • 79,308
  • 64
  • 256
  • 389
3

There are two way to solve index.php in url path for codeigniter

1:In config/config.php Change code :

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

to

$config['index_page'] = '';

2:Create .htacess in root path if not created,Copy the following code:-

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

Save it and then check in your Apache Configuration rewrite_module OR mod_rewrite is Enabled.If not then please enabled. (Best Approach)!

Devraj Gupta
  • 319
  • 3
  • 11
2

i tested this on apache2 on many different hosting and it works great.

use this htaccess

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

be sure you have enabled mod_rewirte with a phpinfo();

then do this in config/config.php:

$config['index_url']    = '';
|
| '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'] = 'AUTO';

if it doesn't works yet, try to change the $config['uri_protocol']='AUTO' to one of the listed inside application/config/config.php file on line 40/54:

sometimes i used : REQUEST_URI instead of AUTO or "QUERY_STRING" for goDaddy hostings

itsme
  • 45,343
  • 90
  • 210
  • 334
2

Look in the \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 like this:

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

It work for me, hope you too.

glider
  • 99
  • 2
  • 8
2

If you are on linux and using apache2 server then we may need to override apache2.conf file also beside changes on .htaccess file. Find apache2 configuration file on /etc/apache2/apache2.conf .

Search Directory /var/www/ Change AllowOverride None -> AllowOverride All

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory
rabin
  • 81
  • 2
1

place a .htaccess file in your root web directory

Whatsoever tweaking you do - if the above is not met - it will not work. Usually its in the System folder, it should be in the root. Cheers!

foxybagga
  • 4,034
  • 2
  • 32
  • 31
  • 2
    After spending couple of hours, I read this and found my mistake. – mohsin.mr Feb 25 '13 at 04:28
  • I also spent at least an hour because of this "little" trick. By default, the .htaccess files comes in `application` directory instead of root. – vadim Feb 12 '14 at 21:49
1

I have tried many solutions but the one i came up with is this:

DirectoryIndex index.php
RewriteEngine on

RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots  \.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]

This iwill remove the index.php from the url as required.

codegeek
  • 91
  • 1
  • 2
  • 8
1

I use these lines in .htaccess file; which I place in root folder

RewriteEngine on
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

Then I can access http://example.com/controller/function/parameter

instead of http://example.com/index.php/controller/function/parameter

Scorpion
  • 396
  • 2
  • 14
0

This will help you paste this code in application folder in .htacess file

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

<Files "index.php">
AcceptPathInfo On
</Files>
<IfModule mod_php5.c>
  php_value short_open_tag 1
</IfModule>
Majid
  • 11
  • 2
0

This one did me the complete magic:

RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

Hope it helps!

bmnepali
  • 341
  • 8
  • 18
0

Hi This one worked me

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

as well as this

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

where the folder is the name of the subfolder if this codeigniter application is hosted as a subdomain e.g domainname/folder/index.php.

Hope that works for you. Thanks.

lightup
  • 534
  • 7
  • 15
0

Copy past following code in your .htaccess

RewriteEngine on
Options -Indexes
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
dev87
  • 94
  • 1
  • 9
  • 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. – Benjamin W. May 23 '16 at 07:42
0
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Foldername of your ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
mohd jagir
  • 51
  • 1
  • 3
0

Step 1 :

Add this in htaccess file

<IfModule mod_rewrite.c> 
RewriteEngine On 
#RewriteBase / 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^ index.php [QSA,L] 
</IfModule>

Step 2 :

Remove index.php in codeigniter config

$config['base_url'] = ''; 
$config['index_page'] = '';

Step 3 :

Allow overriding htaccess in Apache Configuration (Command)

sudo nano /etc/apache2/apache2.conf and edit the file & change to

AllowOverride All

for www folder

Step 4 :

Enabled apache mod rewrite (Command)

sudo a2enmod rewrite

Step 5 :

Restart Apache (Command)

sudo /etc/init.d/apache2 restart
Shashank Saxena
  • 1,834
  • 14
  • 10