You need to enable JavaScript to run this app. Codeigniter 4路由在Godaddy Windows生产服务器仅默认控制器可用问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Codeigniter 4路由在Godaddy Windows生产服务器仅默认控制器可用问题求助

核心问题判定

你所有基于Apache的.htaccess配置在Godaddy Windows服务器上完全不生效:Godaddy Windows主机默认运行IIS服务,仅识别web.config格式的重写规则,根本不会解析.htaccess文件。这就是为什么仅默认入口(public/index.php)能正常访问、其余路径全部返回404的根本原因。


分步解决操作

1. 配置IIS专属重写规则

由于你已经将站点文档根目录直接绑定到httpdocs/public,无需再在httpdocs根目录配置跳转规则:

  • 先删除httpdocs根目录下原有的.htaccess文件,避免路径解析冲突
  • 在httpdocs/public目录(即站点实际运行根目录)下新建web.config文件,写入以下适配CodeIgniter 4的IIS重写规则:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <httpErrors errorMode="Detailed" />
    <rewrite>
      <rules>
        <!-- 禁止目录浏览 -->
        <rule name="Block Directory Browse" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
          </conditions>
          <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Directory access is prohibited" />
        </rule>
        <!-- 自动去掉URL尾部斜杠,返回301重定向 -->
        <rule name="Remove Trailing Slash" stopProcessing="true">
          <match url="(.*)/$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="{R:1}" />
        </rule>
        <!-- 核心路由重写:把所有非静态文件请求转发给CI4入口文件 -->
        <rule name="CI4 Route Rewrite" stopProcessing="true">
          <match url="^(.*)$" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
        </rule>
        <!-- 透传HTTP鉴权头,保障API接口正常获取身份信息 -->
        <rule name="Pass Authorization Header" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_AUTHORIZATION}" pattern=".*" />
          </conditions>
          <serverVariables>
            <set name="HTTP_AUTHORIZATION" value="{HTTP_AUTHORIZATION}" />
          </serverVariables>
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

2. 修正CI4框架的环境适配配置

Windows/IIS环境和本地Apache环境的路径识别逻辑存在差异,需要调整两处核心配置:

  • 打开app/Config/App.php,将$baseURL属性直接配置为线上完整域名(如https://你的域名.com/),不要保留空值让框架自动识别,避免路径匹配错误
  • 调整路由规则的大小写匹配逻辑:Windows服务器的文件系统不区分大小写,但CI4默认路由匹配是区分大小写的,可将原有路由规则中的自定义路径统一为小写写法(如把'/Login'改为'/login'),匹配时可正常识别。

3. 清理无效冗余配置

  • 删除public目录下原有的.htaccess文件,这类Apache专属配置在IIS环境下完全不被解析,无实际作用
  • 确保没有多层跳转规则嵌套,避免出现重定向循环

异常兜底排查

如果配置完成后仍存在404问题,逐一核对以下项:

  • 登录Godaddy主机控制面板,确认IIS的URL Rewrite模块已正常启用(Godaddy Windows主机默认预装该模块,个别旧版主机需要手动在面板开启)
  • 临时将web.config里的errorMode设为Detailed,查看实际报错的具体路径,定位是重写规则不生效还是框架内部路由匹配错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.20 22:36:05