开发环境 Apache 正常,部署到 Lighttpd 后所有链接跳转到首页。原因:Lighttpd 不支持 .htaccess,需在 lighttpd.conf 中配置 url.rewrite-once。
url.rewrite-once = (
# 静态资源直接通过
"^/(css|files|img|js|stats)/" => "$0",
# 其余请求重写到 index.php
"^/([^.]+)$" => "/index.php/$1"
)
url.rewrite-once:按顺序匹配,首个匹配生效,不再继续"^/(css|files|img|js|stats)/" => "$0":静态目录直接访问,不重写"^/([^.]+)$" => "/index.php/$1":无点号的路径(即非静态文件)重写到 index.php/路径,CodeIgniter 解析 URI_PROTOCOL = 'REQUEST_URI' 即可获取控制器/方法<VirtualHost *:80>
DocumentRoot "/var/www/ci_project"
ServerName ci.example.com
<Directory "/var/www/ci_project">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</VirtualHost>
<?php
// application/config/config.php
$config['index_page'] = ''; // 隐藏 index.php
$config['uri_protocol'] = 'REQUEST_URI'; // 关键:从 REQUEST_URI 解析路由
$config['base_url'] = 'https://ci.example.com/';
/etc/lighttpd/lighttpd.conf 加入重写规则sudo systemctl reload lighttpdhttps://ci.example.com/controller/method 确认路由生效/var/log/lighttpd/error.log 排查 500/404🔴 正则未转义点号
.导致匹配任意字符
🔴 静态资源规则放在最后,被通配规则拦截
🔴uri_protocol设为AUTO或PATH_INFO导致解析失败
🔴 缺少AllowOverride All导致 .htaccess 不生效(Apache)
新项目推荐使用 Nginx + PHP-FPM,配置更标准、生态更完善:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
但若必须维护 Lighttpd 环境,上述 url.rewrite-once 规则即可完美解决 CodeIgniter 路由问题。