Cannot show view without index.php in a url except

2019-08-13 22:59发布

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条回答
Fickle 薄情
2楼-- · 2019-08-13 23:40

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]
查看更多
贼婆χ
3楼-- · 2019-08-13 23:53

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/

查看更多
登录 后发表回答