Vultr服务器上ThinkPHP伪静态失效的根本原因是Web服务器未正确接管请求。Apache需启用rewrite模块并设AllowOverride All;Nginx需配置PATH_INFO传递及s参数重写;IIS需完整web.config并安装URL Rewrite模块;同时ThinkPHP必须开启url_route_on。

在 Vultr 的 Linux 服务器(通常是 Ubuntu 或 CentOS)上部署 ThinkPHP 后,伪静态失效、404、index.php 无法隐藏——根本原因不是 ThinkPHP 配置错,而是 Web 服务器没正确接管请求。Vultr 默认不开启重写模块,也不允许 .htaccess 生效,必须手动配置。
Apache 环境下必须启用 rewrite 并放开 AllowOverride
ThinkPHP 的 .htaccess 文件只有在 Apache 允许覆盖时才起作用。Vultr 新装的 Apache 默认是 AllowOverride None,直接忽略项目根目录下的 .htaccess。
- 编辑虚拟主机配置文件,路径通常是
/etc/apache2/sites-available/000-default.conf(Ubuntu)或/etc/httpd/conf.d/vhost.conf(CentOS) - 在
<Directory "/var/www/html">块内添加:AllowOverride AllRequire all granted - 确认已启用 rewrite 模块:
sudo a2enmod rewrite(Ubuntu)sudo systemctl restart apache2 - 项目根目录的
.htaccess内容必须包含完整规则,不能只写RewriteRule:<IfModule mod_rewrite.c><br>RewriteEngine on<br>RewriteCond %{REQUEST_FILENAME} !-d<br>RewriteCond %{REQUEST_FILENAME} !-f<br>RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]<br></IfModule>
Nginx 环境下 location 规则要匹配 PATH_INFO 而非简单转发
Vultr 上很多用户选 Nginx + PHP-FPM,但直接抄 Apache 的 .htaccess 规则会失败——Nginx 不支持 .htaccess,且默认不传递 PATH_INFO。ThinkPHP 5+ 依赖 PATH_INFO 解析路由,不是靠 QUERY_STRING。
- 编辑站点配置,如
/etc/nginx/sites-available/default,在server块内添加:location / {<br> if (!-e $request_filename) {<br> rewrite ^(.*)$ /index.php?s=$1 last;<br> }<br>} - 关键点:用
s=$1而不是/$1,因为 ThinkPHP 的入口逻辑默认读取$_SERVER['PATH_INFO']或$_GET['s'];s=是兼容性更强的 fallback 方式 - 确保 PHP 处理块里有:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param PATH_INFO $fastcgi_path_info; - 重启:
sudo nginx -t && sudo systemctl reload nginx
IIS 环境(极少但存在)web.config 必须补全 conditions 和 action
Vultr 支持 Windows Server 实例,若你真在 IIS 上跑 ThinkPHP(不推荐),官方给的 web.config 片段是残缺的——<conditions> 标签空着,<action> 缺失,IIS 直接跳过规则。
立即学习“PHP免费学习笔记(深入)”;
- 完整可用的
web.config应为:<?xml version="1.0" encoding="UTF-8"?><br><configuration><br><system.webServer><br><rewrite><br><rules><br><rule name="ThinkPHP" stopProcessing="true"><br><match url="^(.*)$" ignoreCase="false"/><br><conditions logicalGrouping="MatchAll"><br><add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/><br><add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/><br></conditions><br><action type="Rewrite" url="index.php?s={R:1}" appendQueryString="true"/><br></rule><br></rules><br></rewrite><br></system.webServer><br></configuration> -
url="index.php?s={R:1}"是核心:把原始路径塞进s参数,ThinkPHP 才能识别 - 确保 IIS 已安装 URL Rewrite Module(需单独下载安装,Windows Server 默认不带)
最容易被忽略的是 ThinkPHP 自身的配置联动:光配好服务器还不够。config.php 中的 'url_route_on' => true 必须开启,'url_html_suffix' => '.html' 这类后缀设置只是锦上添花,真正决定能否走伪静态的是路由开关和服务器是否把请求交给了 index.php。调试时优先检查 $_SERVER['PATH_INFO'] 或 $_GET['s'] 是否有值,而不是看 URL 长什么样。



















