thinkphp 5.0.20 伪静态依赖 web 服务器重写规则(apache 的 .htaccess 或 nginx 的 location 配置),框架仅解析重写后的 url;需启用 mod_rewrite、设置 allowoverride all,nginx 则须配置 try_files 和 fastcgi_param,并关闭 url_route_must、启用 pathinfo 模式。

ThinkPHP 5.0.20 本身不提供“伪静态配置教程”这个独立文档,它的伪静态支持依赖于 Web 服务器(Apache/Nginx)的重写规则,框架只负责识别并解析重写后的 URL。直接找“TP5.0.20 伪静态教程”容易跑偏——真正要配的是 .htaccess 或 Nginx 的 location 块。
Apache 下必须启用 mod_rewrite 并配置 .htaccess
ThinkPHP 自带的 .htaccess 文件在 public/ 目录下,但默认只对 Apache 生效,且要求服务器允许覆盖(AllowOverride All):
- 确认 Apache 已加载
mod_rewrite:检查httpd.conf中LoadModule rewrite_module modules/mod_rewrite.so未被注释 - 确保虚拟主机或目录配置中包含
AllowOverride All(不是None),否则.htaccess被忽略 -
.htaccess内容应类似这样(注意RewriteCond中的括号是英文半角):
<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/xxx 或报 500,大概率是 AllowOverride 没开,或 RewriteBase 缺失(子目录部署时需加 RewriteBase /subdir/)。
Nginx 下不能用 .htaccess,必须改 server 配置
Nginx 不读 .htaccess,所有重写逻辑得写进 server 块里。常见错误是直接复制 Apache 规则,或漏掉 try_files:
- 关键配置段必须包含
try_files $uri $uri/ /index.php?$query_string; - 确保
location ~ \.php$块中fastcgi_param SCRIPT_FILENAME指向正确路径(如$document_root$fastcgi_script_name) - 若项目不在根目录(比如部署在
/tp5/),需在location中加前缀,并同步调整SCRIPT_NAME
典型 Nginx 配置节(放在 server 内):
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
ThinkPHP 侧还要关掉 url_route_must 和检查路由模式
即使 Web 服务器重写成功,TP5.0.20 默认开启强制路由(url_route_must = true),没匹配到路由就直接 404,看起来像伪静态失效:
- 检查
application/config.php中是否设置了'url_route_must' => false(开发调试时建议关掉) - 确认
'url_model'是2(PATHINFO 模式),这是伪静态的基础;值为0(普通模式)或1(兼容模式)时,index.php会强制出现在 URL 中 - 路由文件
route.php里定义的规则,必须和你重写后访问的 URL 路径一致,比如访问/user/list,就得有Route::get('user/list', 'index/user/list');
最常被忽略的一点:TP5.0.20 的伪静态表现,是 Web 服务器、框架路由、入口文件三者共同作用的结果。任一环节出问题(比如 Nginx 没传 QUERY_STRING、Apache AllowOverride 关着、TP 配置里 url_route_must 开着却没写对应路由),都会让 URL 看起来“没生效”,但原因各不相同。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











