You need to enable JavaScript to run this app. CodeIgniter部署至服务器后路由失效 已配置规则仍无法正常访问
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter部署至服务器后路由失效 已配置规则仍无法正常访问

CodeIgniter 部署后路由失效问题排查指南

已同步的现有配置项

  • 根目录 .htaccess 规则:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
  • 框架配置文件核心参数:
$config['base_url_front']  = 'http://example.com; 
$config['base_url']        = 'http://example.com'; 
$config['post_image_url']  = $config['base_url'].'assets/images/post_image/';
$config['post_video_url']  = $config['base_url'].'assets/images/post_video/';
$config['art_image_url']   = $config['base_url'].'assets/images/art_image/';
$config['media_video_url'] = $config['base_url'].'assets/images/media_video/';
$config['parent_folder_name'] = $_SERVER['DOCUMENT_ROOT'].'/';
ServerAdmin webmaster@localhost
ServerName example.com
DocumentRoot /var/www/html/sourcedirectory

<Directory /var/www/html/sourcedirectory/>
    Options +FollowSymLinks
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

针对性排查与解决步骤

  • 第一优先级修正代码硬伤
    检查框架配置文件中$config['base_url_front']的字符串缺少闭合单引号,会直接触发PHP语法解析错误,导致框架初始化流程中断、路由逻辑完全无法加载,补全单引号后重启PHP服务即可验证效果。
  • 确认重写模块加载状态
    站点配置虽然开启了AllowOverride All,但如果服务器未启用mod_rewrite模块,.htaccess中的重写规则不会被执行。执行a2enmod rewrite命令启用模块后,重启Apache服务即可。
  • 适配重写规则兼容场景
    部分使用FastCGI运行PHP的服务器环境中,原重写规则里的?/传值会被服务器层拦截,可将规则调整为兼容版本:
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>
    
    同步检查框架config.php中的$config['index_page']参数,将值设置为空字符串,移除默认的index.php前缀,避免路由匹配时出现前缀拼接错误。
  • 校验文件与目录权限
    确认站点根目录/var/www/html/sourcedirectory下,Apache运行用户(www-data)拥有目录读取权限;同时确认.htaccess文件权限为644,文件所有者与站点运行用户一致,避免因权限不足导致.htaccess规则无法被Apache读取。
  • 校验PATH_INFO兼容配置
    FastCGI模式运行的PHP默认不解析PATH_INFO路径参数,需在站点配置中追加兼容规则,避免路由传递的路径参数被服务器层丢弃。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.07 05:24:07