【问题标题】:Remove index fail in CodeInigter在 CodeIgniter 中删除索引文件
【发布时间】:2017-04-26 03:20:38
【问题描述】:

我想在 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';

以及根目录下的 .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

【问题讨论】:

标签: php .htaccess codeigniter codeigniter-3


【解决方案1】:

在 config.php 中设置您的基本 url,因为它是 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 文件等上无需在底部使用 close ?&gt;

【讨论】:

    【解决方案2】:

    更改 .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>
    

    【讨论】:

      【解决方案3】:

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

      请尝试以下操作:

      在 config.php 中不要更改任何 url 它将保持默认:

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

      以及根目录下的 .htaccess:

      Options +FollowSymLinks
      RewriteEngine on
      
      #Send Request via index.php
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$  index.php/$1 [L]
      

      【讨论】:

      • http://localhost/cantin/danhmuc/add 没有变化,但http://localhost/cantin/index.php/danhmuc/add 可以工作!这是我的 git:github.com/thanhhuy12th/CI3
      猜你喜欢
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多