简体   繁体   中英

Cannot show view without index.php in a url except in the default controller in Codeigniter

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

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/

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM