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

时间:2015-10-13 07:48:29

标签: php codeigniter codeigniter-2

我在CodeIgniter中遇到路由问题,我已经搜索并尝试了stackoverflow上的其他链接,包括Removing index.php in CodeIgniter just works for the default controller URL,但我的问题没有解决。当我从这个url http://localhost/MyProject/运行时,它成功地输出了test.php页面的视图页面,但是问题是假设有test2视图和控制器,所以我不能通过这个url {{ 3}},但我可以通过http://localhost/MyProject/test2加载它。我希望我可以在url之间运行我的代码而不使用index.php,即http://localhost/MyProject/index.php/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 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

<强> FYI

推荐.htaccess

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