Docker环境下CodeIgniter的路由与URL(控制器/方法)无法正常工作
问题排查:访问localhost:8080/main/testfunction出现404错误
访问URL
localhost:8080/main/testfunction时,服务器提示"The requested URL was not found on this server.",但localhost:8080可正常访问并跳转至main/index。当前.htaccess配置如下:<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /booking/ RewriteCond %{REQUEST_URI} ^system.* RewriteRule ^(.*)$ /index.php?/$1 [L] RewriteCond %{REQUEST_URI} ^application.* RewriteRule ^(.*)$ /index.php?/$1 [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L] </IfModule>
问题根源
核心问题出在RewriteBase配置错误以及路径匹配逻辑:
- 你设置的
RewriteBase /booking/意味着服务器会把所有重写请求的基准路径定位到/booking/子目录,但实际访问路径是localhost:8080(根目录),说明项目并没有部署在/booking/下,这直接导致重写规则无法正确映射请求到入口文件。 - 最后一条规则中使用相对路径
index.php?/$1,结合错误的RewriteBase,会让服务器在错误的位置寻找index.php,进一步加剧路径解析错误。
修复方案
步骤1:修正RewriteBase
根据项目实际部署路径调整:
- 如果项目直接部署在Web服务器根目录(即
localhost:8080对应项目根),将RewriteBase改为/; - 如果项目确实在
/booking/子目录下,那访问路径应该是localhost:8080/booking/main/testfunction,否则说明部署路径与配置不匹配。
步骤2:统一入口文件路径
将规则中的入口文件路径改为绝对路径,避免相对路径的解析歧义。
修改后的.htaccess示例(假设项目在Web根目录):
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} ^system.* RewriteRule ^(.*)$ /index.php?/$1 [L] RewriteCond %{REQUEST_URI} ^application.* RewriteRule ^(.*)$ /index.php?/$1 [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?/$1 [L] </IfModule>
补充说明
从localhost:8080能跳转至main/index来看,框架的默认路由已经生效,但重写规则因为基准路径错误,无法处理main/testfunction这类自定义路由请求。修正RewriteBase后,重写规则就能正确将非文件/目录的请求转发到index.php,交由框架路由处理。
内容的提问来源于stack exchange,提问作者Tristan
相关产品推荐
相关产品推荐

