宝塔面板配thinkphp5伪静态需三步:选“thinkphp”模板、运行目录设为/public、确保伪静态规则用last且入口路径匹配;404或index.php暴露主因是三者未对齐。

宝塔面板里配 ThinkPHP5 伪静态,只要三步就能通:选对模板、设对运行目录、确认入口路径匹配。绝大多数 404 或 index.php 强制暴露,都是因为这三点没对齐。
为什么选“ThinkPHP”模板后还是 404
宝塔下拉菜单里的“ThinkPHP”伪静态模板默认生成的是 TP5 兼容规则,但前提是你的项目结构和服务器配置完全匹配。常见断点:
- 站点「运行目录」没设成
/public(必须点进「网站 → 设置 → 运行目录」手动选) -
.htaccess被放错位置——TP5.1+ 入口是public/index.php,.htaccess必须放在public/目录下,不是项目根目录 - Nginx 环境下漏了
fastcgi_param PATH_INFO $fastcgi_path_info;,导致框架启动时报URL pathinfo not supported - Apache 环境下
AllowOverride是None,或rewrite_module没启用
伪静态规则必须用 last,不能用 break
在宝塔「伪静态」文本框里粘贴规则时,跳转控制符决定成败。TP5 默认走 $_GET['s'] 参数解析路由,所以规则必须让请求重写后重新进入 location ~ \.php$ 流程:
- ✅ 正确:
if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; } - ❌ 错误:
rewrite ^(.*)$ /index.php?s=$1 break;—— 重写后不重新匹配 location,Nginx 直接尝试返回/index.php?s=xxx源码或 404 - ⚠️ 注意:规则中
/index.php指向的是 public 目录下的入口,所以你得确保宝塔「运行目录」已设为/public,否则这个路径就错了
Apache 用户要额外检查两个硬开关
TP5 在 Apache 下依赖 .htaccess + mod_rewrite,但宝塔或 phpEnv 默认常关掉关键项:
- 进「Apache 设置 → 模块管理」,勾选
rewrite_module - 打开站点 vhost 配置(如
phpenv\apache\vhosts\yourdomain.conf),确认有且仅有一个<directory></directory>块,并含AllowOverride All -
.htaccess内容推荐用这一版(放在public/下):<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>注意末尾是index.php?/$1,不是index.php/$1;后者在部分 Apache 版本下会丢参数
最常被忽略的其实是运行目录和入口路径的耦合关系:宝塔里设了 /public,规则里写 /index.php 才真正指向 public/index.php;如果运行目录设错,/index.php 就会变成找根目录下的文件,而它根本不存在。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











