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'].'/';
- 项目目录结构参考:项目目录结构截图
- Apache 站点虚拟主机配置:
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
相关产品推荐
相关产品推荐

