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

27

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?

Pennell answered 18/9, 2009 at 15:46 Comment(1)
formget.com/codeigniter-htaccess-remove-index-phpHillman
D
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/

Drake answered 18/9, 2009 at 15:53 Comment(5)
+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]Pennell
for some reason, i had to add a slash to the beginning of the RewriteCond. RewriteCond $1 !^\/(index\.php|assets|robots\.txt)Alica
For some reason, if you have index.php in the URL, it's not actually removed, it stays there and keeps working.Naif
I used all the possible combinations and it's still not working for me!!!!! how did you get it to work??Blackington
Your Apache install needs to be configured to check .htaccess files - make sure that is the case.Drake
P
61

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'] = '';
Preferable answered 28/10, 2011 at 14:53 Comment(1)
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.Hebrew
D
32

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';
Denominative answered 8/9, 2012 at 10:47 Comment(3)
thank you , i have been failing in this for long time dunno why but ur way worked like butter :DUund
Thanks. It's works. BTW, I needn't change config.php.Argentic
Thanks, this one helped me, I used PATH_INFO with WAMPGeis
I
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

Israelisraeli answered 18/9, 2009 at 15:52 Comment(0)
H
9

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>
    
     ...
Hyalo answered 18/7, 2012 at 20:31 Comment(2)
Thank you so much, i got that AllowOverride None too, now it works !Bogoch
The blog is gone now, and I would very much have liked to read it. None of these solutions are working for me...Incompetence
P
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.

Prudie answered 21/1, 2013 at 13:23 Comment(0)
S
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]
Stern answered 4/9, 2014 at 9:24 Comment(2)
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 outLaurentia
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 :)Coelacanth
N
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”
Nealneala answered 6/1, 2012 at 10:30 Comment(0)
B
5

This will work:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 
Beaudoin answered 15/11, 2012 at 12:46 Comment(0)
C
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]  
Charming answered 29/4, 2013 at 13:50 Comment(1)
Another module I needed to enable was actions: #14420257Winton
H
4

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] 
Howlan answered 10/6, 2014 at 18:58 Comment(0)
G
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)!

Groff answered 18/9, 2009 at 15:46 Comment(1)
This is correct and straight from the CodeIgniter docs. I also using this method.Hurlow
T
3

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

Tauromachy answered 18/9, 2009 at 15:47 Comment(0)
D
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.

Danielldaniella answered 18/9, 2009 at 15:51 Comment(0)
M
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.

Mae answered 11/10, 2013 at 15:48 Comment(0)
F
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

Fluorite answered 18/9, 2009 at 15:46 Comment(0)
P
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]
Paramo answered 24/7, 2013 at 6:39 Comment(0)
F
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
Fanaticize answered 17/2, 2015 at 2:23 Comment(0)
P
2

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
Pluralize answered 23/8, 2019 at 6:43 Comment(0)
B
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.

Byroad answered 3/9, 2012 at 12:18 Comment(2)
After spending couple of hours, I read this and found my mistake.Aldos
I also spent at least an hour because of this "little" trick. By default, the .htaccess files comes in application directory instead of root.Medwin
M
1

This will remove the index.php from the url as required:

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]
Mesomorph answered 14/2, 2014 at 11:53 Comment(0)
P
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

Pustulant answered 14/9, 2015 at 11:26 Comment(0)
Q
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>
Quahog answered 10/9, 2013 at 7:29 Comment(0)
J
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]
Jett answered 4/10, 2015 at 6:21 Comment(0)
M
0

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.

Mormon answered 18/3, 2016 at 13:5 Comment(0)
D
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]
Dade answered 23/5, 2016 at 7:23 Comment(1)
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.Commercialize
J
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>
Jada answered 15/11, 2018 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.