9

I tried everything on google related to removing index.php from URL in codeigniter but no solution worked for me I am using codeigniter version 2.1.0 How I can remove index form my URL ?

Thanks

applechief
  • 6,615
  • 12
  • 50
  • 70
Faryal Khan
  • 881
  • 4
  • 19
  • 37

9 Answers9

22

In application/config.php make sure that

$config['index_page'] = "";

Then set this as your .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule> 

You're all set!

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
Raphael Caixeta
  • 7,808
  • 9
  • 51
  • 76
5

For me, Raphael Caixetas solution didn't work, but this did :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 
eric.itzhak
  • 15,752
  • 26
  • 89
  • 142
  • Didn't work for me at first. Then I figured out that you have to enable `mod_rewrite` in Apaches `httpd.conf` for it to work. Works perfectly now, thanks :) – Dvir Levy Dec 06 '12 at 19:03
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_page']   = '';
|
| '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

Daniel Luca CleanUnicorn
  • 1,087
  • 1
  • 12
  • 30
itsme
  • 48,972
  • 96
  • 224
  • 345
1

check your httpd.conf file

change

AllowOverride None

to

AllowOverride All
user2429569
  • 91
  • 2
  • 8
1

Have you tried using the example given in the CodeIgniter user guide?

By default, the index.php file will be included in your URLs:

example.com/index.php/news/article/my_article

You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:

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

In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.

http://codeigniter.com/user_guide/general/urls.html

Stanley
  • 3,935
  • 2
  • 18
  • 22
0

This is what I use and works. Use this code in your .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* website/index.php/$0 [PT,L] 

Just make sure the .htaccess is in your root folder. (ie: xampp/htdocs)

Then in your config.php(application\config) replace config['index_page']='index.php' to config['index_page']=''.

Robert
  • 186
  • 5
  • 13
0

Don't forget to change de config file!

application/config.php

$config['index_page'] = '';

.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Slipstream
  • 13,455
  • 3
  • 59
  • 45
0

You just need to create .htaccess file in project folder and write: RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L] 

</IfModule>
<IfModule !mod_rewrite.c>
 ErrorDocument 404 index.php
 </IfModule>

 And You don't need to define in base_url in config file:
 $config['base_url'] = '';// blank it.

 I hope it will work perfectly ...
Jay Kareliya
  • 760
  • 7
  • 21
0

In addition of the .htaccess file given by Robert Stanley (see his answer), go to application/config/config.php, look for $config['index_page'] = "index.php"; and replace it with $config['index_page'] = "";

nullability
  • 10,545
  • 3
  • 45
  • 63
Hassen
  • 6,966
  • 13
  • 45
  • 65