1

I have a routing issue in CodeIgniter, i have searched and tried other links on stackoverflow including Removing index.php in CodeIgniter just works for the default controller URL but my problem did'nt solved. When i run from this url http://localhost/MyProject/ it successfully gives output the view page of test.php page, but the problem is suppose there is test2 view and controller also, so i can't load it directly by this url http://localhost/MyProject/test2 , but i can load it through http://localhost/MyProject/index.php/test2. I want that i can run my code without index.php in between url i.e. by http://localhost/MyProject/test2.

View

test.php

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>  
    <title>Welcome Test </title>
</head>
<body>
    <div>
       <span>Test 1</span>
    </div>
</body>
</html>

Controller

test.php

<?php
class test extends CI_Controller{

    function __construct(){
        parent::__construct();
    }

    public function index(){
        $this->load->view('test');
    }
}
?>
routes.php
$route['default_controller'] = "test";

.htaccess

<IfModule mod_rewrite.c> 
 RewriteEngine On
 #rename "MyProject/" with your application directory 
 #For example you rename the entire CodeIgniter application as "mysite" 
 #then, the "RewriteBase /" will be like this "RewriteBase /mysite" 
 #same as at line number 10, "RewriteRule ^(.*)$ /MyProject//index.php #/$1 [L]" will be like this "RewriteRule ^(.*)$ /mysite/index.php/$1 [L]"
 RewriteBase /MyProject/
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ /MyProject/index.php/$1 [L]
</IfModule>

2 Answers 2

2

You need to remove index.php from your url.Create .htacces file and place in your root.Copy this code and run

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

for more how to remove index.php form url try this http://w3code.in/2015/09/how-to-remove-index-php-file-from-codeigniter-url/

Sign up to request clarification or add additional context in comments.

5 Comments

i tried your code and typed the following url localhost/MyProject/test it is giving error 404 page not found.
but through localhost/MyProject/index.php/test it is working i don't want index.php in between.
Open config.php file insie the config folder and replace $config['index_page'] = "index.php" to $config['index_page'] = ""
try to replcae $config['uri_protocol'] = "AUTO" //replace with the below code $config['uri_protocol'] = "REQUEST_URI"
the issue is same, is there any problem with .haccess file: <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule>
0

FYI

Recommended .htaccess is

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.