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

IIS 10服务器中CodeIgniter 4.3.2首页控制器报404错误求助

解决CodeIgniter 4.3.2在IIS 10上控制器访问404的方案

1. 核对控制器命名与位置

  • 控制器文件必须放在app/Controllers目录下,文件名为大驼峰格式(如Home.php,不能是home.php)
  • 类名需与文件名一致并继承BaseController,示例代码:
    <?php namespace App\Controllers;
    
    use CodeIgniter\Controller;
    
    class Home extends BaseController
    {
        public function index()
        {
            return view('welcome_message');
        }
    }
    
  • 确保控制器方法为public权限,不能是private或protected

2. 检查核心配置文件

  • 编辑app/Config/App.php:
    • 确认baseURL设置正确,末尾必须加斜杠:
      public $baseURL = 'http://localhost/app/';
      
    • 若要去掉index.php访问,设置public $indexPage = '';;若保留index.php,设置为public $indexPage = 'index.php';

3. 修正IIS URL重写配置

情况1:网站根目录为CI4项目根(localhost/app对应项目根目录)

将以下web.config放在项目根目录下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="CodeIgniter Rewrite" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="public/index.php/{R:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
        <defaultDocument>
            <files>
                <clear />
                <add value="public/index.php" />
            </files>
        </defaultDocument>
    </system.webServer>
</configuration>

情况2:网站根目录为CI4的public目录(localhost/app对应public目录)

将以下web.config放在public目录下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Remove index.php" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
  • 确保IIS已安装URL重写模块:打开IIS管理器查看网站功能视图,若没有该模块,需通过服务器管理器安装「Web服务器」角色下的「URL重写」组件

4. 验证路由配置

  • 编辑app/Config/Routes.php:
    • 若开启自动路由($routes->setAutoRoute(true);),确认控制器方法存在且为public
    • 若关闭自动路由(默认状态),需手动添加路由规则:
      $routes->get('home', 'Home::index');
      $routes->get('home/index', 'Home::index');
      
  • 清除路由缓存:删除writable/cache目录下的所有文件,避免旧缓存导致冲突

5. 排查权限与缓存问题

  • 确保IIS应用程序池身份对app/writable目录拥有读写权限
  • 切换至开发模式:在.env文件中设置CI_ENVIRONMENT = development,关闭生产环境缓存以方便排查错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 11:33:19