在CodeInigter中删除索引失败

时间:2017-04-26 03:20:38

标签: php .htaccess codeigniter codeigniter-3

我想删除CI 3.x中的index.php,我跟随google中的所有tut,这是我的代码:

控制器

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');
class Danhmuc extends CI_Controller {
public function __construct()
{
    parent::__construct();
    //Load Dependencies

}
// List all your items
public function index( $offset = 0 )
{

}
public function add()
{
    $this->load->view('danhmuc/danhmuc');
}
}
?>

在views / danhmuc / danhmuc.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
  Hello world
</body>
</html>

在config.php中

$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

root中的.htaccess

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

但是当我尝试http://localhost/cantin/danhmuc/add时,它的显示404未找到。但是当我尝试http://localhost/cantin/index.php/danhmuc/add它的工作时。希望你帮帮我。谢谢!

我的git:https://github.com/thanhhuy12th/CI3

3 个答案:

答案 0 :(得分:0)

更改.htaccess文件

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|images|asset|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

答案 1 :(得分:0)

在config.php中设置基本网址,因为它是CI 3版本及以上所需的

$config['base_url'] = 'http://localhost/yourprojectfolder/';
$config['index_page'] = '';

// Auto no longer in CI3

$config['uri_protocol'] = 'REQUEST_URI';

在routes.php上

$route['danhmuc/add'] = 'danhmuc/add';
$route['danhmuc/(:any)'] = 'danhmuc/index/$1';

https://www.codeigniter.com/user_guide/general/routing.html#examples

对于你的htaccess尝试其中的一些

https://github.com/wolfgang1983/htaccess_for_codeigniter

的.htaccess

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

或者

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

控制器

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Danhmuc extends CI_Controller {

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

    public function index( $offset = 0 ) {
    }

    public function add(){
        $this->load->view('danhmuc/danhmuc');
    }
}
  

在控制器,模型,库,助手,_lang文件等上,无需在底部使用关闭?>

答案 2 :(得分:-1)

您似乎想从网址中删除 index.php 。这很简单。

请尝试以下:

在config.php中,不要为url更改任何内容,它将保持默认值:

$config['index_page'] = 'index.php';
$config['uri_protocol'] = 'REQUEST_URI';

root中的.htaccess:

Options +FollowSymLinks
RewriteEngine on

#Send Request via index.php

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