You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter 3子文件夹路由配置:Linux正常Windows报404错误

CodeIgniter 3 子文件夹路由跨系统(Linux/Windows)404问题排查与解决

核心问题出在Linux和Windows的文件大小写敏感性差异,以及服务器配置的细微区别,以下是针对性的解决步骤:

1. 严格匹配控制器/文件夹的大小写

Linux区分文件名大小写,Windows不区分,但CI的路由解析逻辑会严格匹配路径大小写:

  • 子文件夹(比如application/controllers/admin)保持小写,控制器文件名必须首字母大写(比如Admin.php,不是admin.php)
  • 控制器类名必须和文件名完全一致:
    // application/controllers/admin/Admin.php
    class Admin extends CI_Controller {
        public function index() {
            // 业务逻辑
        }
    }
    
  • 路由规则里的路径要和实际文件夹/控制器的大小写完全匹配:
    // application/config/routes.php
    $route['admin'] = 'admin/Admin/index';
    $route['admin/(:any)'] = 'admin/Admin/$1';
    

2. 修正.htaccess配置适配Windows

如果使用Apache,Windows下的URL重写需要调整:

  • 确保Apache开启了mod_rewrite模块(打开httpd.conf,去掉LoadModule rewrite_module modules/mod_rewrite.so前的注释)
  • 调整.htaccess的RewriteBase和规则:
    RewriteEngine On
    RewriteBase /  # 如果项目在根目录;如果是子目录,改成/你的项目名/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    
  • 检查Apache的目录权限配置,确保AllowOverride All生效:
    # httpd.conf中找到你的项目目录配置
    <Directory "C:/xampp/htdocs/ci-project">
        AllowOverride All
        Require all granted
    </Directory>
    

3. 调整CI核心配置

打开application/config/config.php:

  • 清空index_page,去掉URL里的index.php:
    $config['index_page'] = '';
    
  • 设置合适的uri_protocol,Windows下REQUEST_URI更稳定:
    $config['uri_protocol'] = 'REQUEST_URI';
    

4. 快速测试定位问题

  • 先访问带index.php的URL(比如http://localhost/index.php/admin),如果能正常打开,说明路由和控制器没问题,问题出在URL重写配置
  • 如果还是404,检查控制器文件是否放在正确的子文件夹下,类名是否拼写正确(包括大小写)

内容的提问来源于stack exchange,提问作者Jatin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 17:10:26