无法在 url 中显示没有 index.php 的视图,但在 Codeigniter 中的默认控制器中除外

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

我在 CodeIgniter 中遇到路由问题,我搜索并尝试了 Whosebug 上的其他链接,包括 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 #/ [L]" will be like this "RewriteRule ^(.*)$ /mysite/index.php/ [L]"
 RewriteBase /MyProject/
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ /MyProject/index.php/ [L]
</IfModule>

您需要从 url.Create .htaccess 文件中删除 index.php 并将此代码放入 root.Copy 和 运行

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

有关如何删除 index.php 表单 url 的更多信息,试试这个 http://w3code.in/2015/09/how-to-remove-index-php-file-from-codeigniter-url/

仅供参考

推荐.htaccess

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