CodeIgniter removing index.php from url

Php.HtaccessCodeigniterApache2

Php Problem Overview


My current urls look like this [mysite]index.php/[rest of the slug].
I want to strip index.php from these urls.

mod_rewrite is enabled on my apache2 server. In config, $config['index_page'] = '';

My codeignitor root .htaccess file contains,

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

But still it is not working. Where am I going wrong?

Php Solutions


Solution 1 - Php

Try the following

Open config.php and do following replaces

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

to

$config['index_page'] = ""

In some cases the default setting for uri_protocol does not work properly. Just replace

$config['uri_protocol'] ="AUTO"

by

$config['uri_protocol'] = "REQUEST_URI"

.htaccess

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

Note: .htaccess code vary depending on hosting server. In some hosting server (e.g.: Godaddy) need to use an extra ? in the last line of above code. The following line will be replaced with last line in applicable case:

// Replace last .htaccess line with this line
RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 

Solution 2 - Php

Step:-1 Open the folder “application/config” and open the file “config.php“. find and replace the below code in config.php file.

//find the below code   
$config['index_page'] = "index.php" 
//replace with the below code
$config['index_page'] = ""

Step:-2 Go to your CodeIgniter folder and create .htaccess

Path:
Your_website_folder/
application/
assets/
system/
.htaccess <——— this file
index.php

Step:-3 Write below code in .htaccess file

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

Step:-4 In some case the default setting for uri_protocol does not work properly. To solve this problem just open the file “application/config/config.php“, then find and replace the below code

//Not needed for CodeIgniter 3 its already there.
//find the below code
$config['uri_protocol'] = "AUTO"
//replace with the below code
$config['uri_protocol'] = "REQUEST_URI" 

Thats all but in wamp server it does not work because rewrite_module by default disabled so we have need to enable it. for this do the following

  1. Left click WAMP icon
  2. Apache
  3. Apache Modules
  4. Left click rewrite_module

Original Documentation

Example Link

Solution 3 - Php

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

Solution 4 - Php

> 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]

Solution 5 - Php

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

use

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

if you have url in this order site.com/index.php/class/method/id

> NB:remove index.php in config.php should be config[index_page] = "" > > NB: notice the difference at the last line instead of $0 use $1

Solution 6 - Php

on your htaccess use this,

RewriteEngine On
RewriteBase /root_folder_name/
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Hope this helps.

Solution 7 - Php

There are two steps:

  1. Edit config.php

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

To

$config['index_page'] = '’;

2) Create/Edit .htaccess file

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

Solution 8 - Php

Solved it with 2 steps.

> 1. Update 2 parameters in the config file application/config/config.php

$config['index_page'] = '';
$config['uri_protocol']	= 'REQUEST_URI';

> 2. Update the file .htaccess in the root folder

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

Solution 9 - Php

1.Create a new file .htaccess on root directory.

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

2. Go to your config.php file and change from $config['index_page'] = 'index.php' to $config['index_page'] = ''

Solution 10 - Php

  1. make .htaccess file and put this code

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

     $config['index_page'] = ''; 
    

remove index.php present in config.php file

  1. rewrite on from your server.

Solution 11 - Php

try this one.It may help you as its working for me.

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

put the code in your root htaccess file.and make sure that you have

$config['index_page'] = '';

in your config file.

Please let me know if you face any problem.

Solution 12 - Php

+ Copy .htaccess from Application to Root folder
+ edit .htaccess and put

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /ci_test/

    #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>

Note: make sure your web server is enable for  "rewrite_module"

+ Remove index.php from $config['index_page'] = 'index.php';
- edit config.php in application/config
- search for index.php
change it from  $config['index_page'] = 'index.php'; to  $config['index_page'] = '';

Please watch this videos for reference https://www.youtube.com/watch?v=HFG2nMDn35Q

Solution 13 - Php

Well these all answers are good but for a newbie(which is do first time) these are confusing. There are other htaccess files which is reside in controller but we have to edit or add the htaccess in main project folder.

This is your codeigniter folder, where file resides like application,system,assets,uploads etc.

Your_project_folder/
application/
assets/
cgi-bin/
system/
.htaccess---->(Edit this file if not exist than create it)
index.php

Change this file as written below:

<IfModule mod_rewrite.c>
  RewriteEngine On
  # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
  #  slashes.
  # If your page resides at http://www.example.com/mypage/test1
  # then use RewriteBase /mypage/test1/
  # Otherwise /
  RewriteBase /
  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: Anand Pandey
  ErrorDocument 404 /index.php
</IfModule>

and yes of course you need to change two lines of code in the application/config/config.php

//find the below code   
$config['index_page'] = "index.php";
$config['uri_protocol'] ="AUTO" 
//replace with the below code
$config['index_page'] = "";
$config['uri_protocol'] = "REQUEST_URI";

Solution 14 - Php

you can go to config\config.php file and remove index.php value from this variable

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

create .htaccess file and paste this code in it.

<IfModule mod_rewrite.c>
 
 Options +FollowSymLinks

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

</IfModule>

good chance

Solution 15 - Php

Both development and production

    # Development
    RewriteEngine On
    RewriteBase /your_local_folder/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
    RewriteRule ^(.*)$ index.php/$1 [L]
    
    #Production PHP5 CGI mode
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
    RewriteRule ^(.*)$ index.php?/$1 [L]

Solution 16 - Php

first of all you have to on the apache server rewrite engine on.this link will help you for that

http://www.anmsaiful.net/blog/php/enable-apache-rewrite-module.html

RewriteEngine On
RewriteBase /your_folder_name/

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

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

this will be your .htaccess file. now try it .this setting works for me.

Solution 17 - Php

  Removing index.php from url of codeigniter   

Three steps are require to remove index.php from url in Codeigniter.

1)Create .htacess file in parallel to application holder and just copy past the following code:

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

2) Change $config['index_page'] to blank in config.php in application folder as below:

$config['index_page'] = '';

3) Enable “rewrite_module” of apache.

Solution 18 - Php

Only three steps are require to remove index.php from url in Codeigniter

  1. Create .htacess file in parallel to application holder and just copy past the following code:

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

  2. Change $config['index_page'] to blank in config.php in application folder as below:

    $config['index_page'] = '';

  3. Enable “rewrite_module” of apache.

Restart your apache and its done.

Details : http://sforsuresh.in/removing-index-php-from-url-of-codeigniter-in-wamp/

Solution 19 - Php

I think your all setting is good but you doing to misplace your htaccess file go and add your htaccess to your project file

project_folder->htaccess

and add this code to your htaccess

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

Solution 20 - Php

you can go to application\config\config.php file and remove index.php

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

Change to

$config['index_page'] = '';

Solution 21 - Php

You can remove index.php from url by placing some code in .htaccess

File path: root > .htacess

RewriteEngine On
RewriteBase /

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

Hope it will works.

Solution 22 - Php

try this RewriteEngine On

        # Removes index.php from ExpressionEngine URLs
        RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

Solution 23 - Php

This works for me. Tested in codeigniter 3.1.11, PHP 5.6.40 I use a virtualhost in my apache2 config in testing server.

<VirtualHost *:80>
     ServerAdmin [email protected]
     DocumentRoot (__DIR__)
     ServerName mydomain
     ##ErrorLog "logs/dummy-host2.example.com-error.log"
     ##CustomLog "logs/dummy-host2.example.com-access.log" common
     <Directory "(__DIR__)">
		Require all granted
        AllowOverride All
   </Directory>
</VirtualHost>

And /.htaccess content

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

Solution 24 - Php

Add .htaccess on your root folder and past the following code:

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

Solution 25 - Php

Follow these step your problem will be solved

1- Download .htaccess file from here https://www.dropbox.com/s/tupcu1ctkb8pmmd/.htaccess?dl=0

2- Change the CodeIgnitor directory name on Line #5. like my directory name is abc (add your name)

3- Save .htaccess file on main directory (abc) of your codeignitor folder

4- Change uri_protocol from AUTO to PATH_INFO in Config.php file

Note: First of all you have to enable mod_rewrite from httpd.conf of apachi by removing the comments

Solution 26 - Php

Use this code. Change the 2nd line as you have your folder name. Where index.php file and folders application, system folder lie. Mostly problems happen due to second line. we don't configure the folder name where project is place. Important is 2nd line. This is simple to remove the index.php. RewriteEngine on RewriteBase /project_folder_name RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php/$0 [PT,L]

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

Go to controller/config.php file.

config['index_url']='';

Reference for more study.

https://ellislab.com/expressionengine/user-guide/urls/remove_index.php.html

https://ellislab.com/blog/entry/fully-removing-index.php-from-urls

Solution 27 - Php

Besides these answers, one can just, add index.php or edit it (if it is already there) - for security you want to disable index.php anyway, from listing the files in your website directory - then change the content into

<?php
   header('location:your_required_starting.php'); 
?>

Solution 28 - Php

Minor thing: (Optional)

> Try restarting your Apache after updating the rewrite engine status in vhosts as Rewrite ON.

One critical thing to notice:

>In Apache vhosts file, check whether you have this script line AllowOverride None If yes, please remove/comment this line.

My vhosts file script: (Before)

<VirtualHost *:80>
    DocumentRoot "/path/to/workspace"
    ServerName ciproject
    ErrorLog "/path/to/workspace/error.log"
    CustomLog "/path/to/workspace/access.log" common
    Directory   "/path/to/workspace">
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory> 
</VirtualHost>

My vhosts file script: (After)

<VirtualHost *:80>
    DocumentRoot "/path/to/workspace"
    ServerName ciproject
    ErrorLog "/path/to/workspace/error.log"
    CustomLog "/path/to/workspace/access.log" common
    Directory   "/path/to/workspace">
        Options Indexes FollowSymLinks
        #AllowOverride None
        Order allow,deny
        Allow from all
    </Directory> 
</VirtualHost>

I faced the same problem, struggled for around 1 day, all were fine. Later, by trial and error method, found this thing. After removing this, my job was accomplished. The URL now does not look up for index.php :)

Solution 29 - Php

if all the above solution not work then in your project folder their will be two files index.html and index.php, rename index.html to index_1.html and browse your project.

Solution 30 - Php

Note the difference with the added "?" character after ".php", especially when dealing with CodeIgniter:

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

vs.

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

It depends on several other things.. if doesn't work, try the other option!

Solution 31 - Php

You can eliminate index.php from URL by create .htaccess file in the base directory of your porject, that file its content as

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]

save that file also your changed in config.php file is right

$config['index_page'] = '';

I wish to run successfully

Solution 32 - Php

In CodeIgniter 3.16 in using htaccess to

> remove index.php file

& also

> redirect HTTP to HTTPS

Follow are the codes:

  • go to application\config\config.php file

Make changes

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

To

$config['index_page'] = '';
  • Now, Open the .htaccess file, and update the codes below
RewriteEngine on
RewriteBase /

## Redirect SSL
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

## Remove fron url index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^/(index\.php|assets/|humans\.txt)
RewriteRule ^(.*)$ index.php/$1 [L] 

Solution 33 - Php

  1. Create a .htaccess file in root directory (change RewriteBase by project name)

After creating the .htaccess file, add the following code to this file.

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

2. Removing index.php file (IN CONFIG.PHP FILE)

Open the config.php file in your text editor and remove index.php from here.

Change:

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

But in a few cases, it may happen that the default setting for url_protocol does not work properly. For solving this problem we need to open config.php and

Change: according to your project name

$config['base_url'] = 'http://localhost:8082/CodeIgniter/';


[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/1Ew3Q.png

Solution 34 - Php

You have to place the .htaccess in the htdocs folder, and the part of

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

must be written like this:

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

Yeah I know that if you have other projects in xampp they will be messed up :(

When you upload the site to a remote server you can remove /your_codeigniter_project_folder/ from the .htaccess file contents

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNihar SarangiView Question on Stackoverflow
Solution 1 - PhpRahul TailwalView Answer on Stackoverflow
Solution 2 - PhpMaxEchoView Answer on Stackoverflow
Solution 3 - PhpDino BabuView Answer on Stackoverflow
Solution 4 - PhpvinodView Answer on Stackoverflow
Solution 5 - Phpuser3464848View Answer on Stackoverflow
Solution 6 - Phpyuvaraj bathrabaguView Answer on Stackoverflow
Solution 7 - PhpshafiView Answer on Stackoverflow
Solution 8 - PhpKhachornchit SongsaenView Answer on Stackoverflow
Solution 9 - PhpManish SharmaView Answer on Stackoverflow
Solution 10 - PhpSiddharth ShuklaView Answer on Stackoverflow
Solution 11 - PhpABortyView Answer on Stackoverflow
Solution 12 - PhpKH SolveCodeView Answer on Stackoverflow
Solution 13 - PhpAnand PandeyView Answer on Stackoverflow
Solution 14 - Phpabbas hussenView Answer on Stackoverflow
Solution 15 - PhpcgwCodeView Answer on Stackoverflow
Solution 16 - Phpvaibhav kulkarniView Answer on Stackoverflow
Solution 17 - PhpPritam ChaudhariView Answer on Stackoverflow
Solution 18 - PhpSuresh KamrushiView Answer on Stackoverflow
Solution 19 - PhpYaseen AhmadView Answer on Stackoverflow
Solution 20 - PhpRafiqul IslamView Answer on Stackoverflow
Solution 21 - PhpRokan NashpView Answer on Stackoverflow
Solution 22 - PhpKashifView Answer on Stackoverflow
Solution 23 - Phpjose insfranView Answer on Stackoverflow
Solution 24 - PhpAbd AbughazalehView Answer on Stackoverflow
Solution 25 - PhpSalman KhanView Answer on Stackoverflow
Solution 26 - PhpHafiz Shehbaz AliView Answer on Stackoverflow
Solution 27 - PhpbennyhardjonoView Answer on Stackoverflow
Solution 28 - PhpLalith KumarView Answer on Stackoverflow
Solution 29 - PhpMohsin ShoukatView Answer on Stackoverflow
Solution 30 - Phpeyal_katzView Answer on Stackoverflow
Solution 31 - PhpRabab ShView Answer on Stackoverflow
Solution 32 - PhpChandan SharmaView Answer on Stackoverflow
Solution 33 - PhpSithum WitharanaView Answer on Stackoverflow
Solution 34 - Php19CM059 Vedika SontakkeView Answer on Stackoverflow