CentOS下ThinkPHP5伪静态失效主因是Apache的AllowOverride未设为All、.htaccess未放于public目录,或Nginx漏配fastcgi_param PATH_INFO;需同步确保mod_rewrite启用、运行目录为/public、SELinux允许runtime写入。

CentOS 下 ThinkPHP5 伪静态配不起来,90% 是因为 Apache 的 AllowOverride 没开、.htaccess 放错位置,或 Nginx 漏了 fastcgi_param PATH_INFO —— 不是规则本身有问题。
Apache:.htaccess 不生效的三个硬性条件
ThinkPHP5 默认入口在 public/index.php,.htaccess 必须放在 public/ 目录下,不是项目根目录(含 application 那层)。
-
mod_rewrite模块必须启用:执行a2enmod rewrite,再systemctl restart httpd - 对应站点的
<directory></directory>块中,AllowOverride必须设为All(不能是None或FileInfo) -
.htaccess内容末尾用index.php?/$1,不是index.php/$1;否则部分 Apache 版本会丢掉查询参数
推荐规则(放在 public/.htaccess 中):
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>
Nginx:404 或报 “URL pathinfo not supported” 的根源
TP5 依赖 $_SERVER['PATH_INFO'] 解析路由,但 Nginx 默认不传这个变量。只改 try_files 不补 fastcgi_param,框架启动就中断。
立即学习“PHP免费学习笔记(深入)”;
-
location /块必须指向/public/index.php,不是/index.php -
location ~ \.php$块里必须显式添加:fastcgi_param PATH_INFO $fastcgi_path_info; - 宝塔用户务必进「网站设置 → 运行目录」选
/public,否则runtime/写入失败,后续全挂
关键配置片段(Nginx server 块内):
location / {
try_files $uri $uri/ /public/index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
框架层:url_route_on 和 pathinfo_depr 必须对齐服务器行为
光配服务器没用。TP5 若读不到 PATH_INFO,会退到 $_GET['s'] 模式,但此时服务器没把路径塞进 s 参数,就直接 404。
- 确认
config/app.php中:'url_route_on' => true,且'pathinfo_depr' => '/' - 不要设
'url_route_must' => true初期调试,否则没定义的路由直接报 404,掩盖真实问题 - 如果用了子目录部署(如
http://domain.com/myapp/),Apache 要加RewriteBase /myapp/,Nginx 的try_files路径也要相应调整
最易被忽略的一点:CentOS SELinux 默认阻止 Apache/Nginx 写入 runtime/ 目录。哪怕所有配置都对,也会卡在缓存写入失败。临时验证可执行 setenforce 0,长期方案是 chcon -R -t httpd_sys_rw_content_t runtime/。



















