Apache未启用mod_rewrite或未允许.htaccess解析导致Laravel/ThinkPHP路由404;需执行a2enmod rewrite、配置AllowOverride All并重启服务;nginx则需正确设置try_files和PHP处理器。
直接结论:不是 PHP 本身的问题,是 Apache 的 mod_rewrite 模块没开,或 .htaccess 未被允许解析。PHP 脚本能正常执行,但所有非 index.php 的 URL 都会 404 —— 因为重写规则根本没生效。
a2enmod rewrite(Debian/Ubuntu);若提示已启用,再确认
/etc/apache2/mods-enabled/rewrite.load 存在且内容为 LoadModule rewrite_module modules/mod_rewrite.so
块,在 DocumentRoot 下方加:(路径按实际调整)AllowOverride All Require all granted
systemctl restart apache2,别只 reload
nginx 没有“rewrite 模块开关”的概念,它默认支持 rewrite 指令,但 Laravel、TP 等框架的“伪静态”依赖的是 try_files 回退到 index.php。404 是因为请求没落到 PHP 处理器,而是被 nginx 当作静态文件找不到了。
location / 块包含:location / {
try_files $uri $uri/ /index.php?$query_string;
}location ~ \.php$ 块存在且正确指向 fastcgi_pass(如 127.0.0.1:9000 或 unix:/var/run/php/php8.1-fpm.sock)http://site.com/app/),try_files 第三项得改成 /app/index.php?$query_string,否则 PATH_INFO 解析错乱有人改了 php.ini 或 www.conf 后“突然 404”,其实不是 rewrite 引起的——而是 FPM 没把请求传给 PHP,或者 PHP 返回了空响应被 nginx/Apache 当成 404。这类问题容易误判。
fastcgi_param SCRIPT_FILENAME 是否拼错(常见错写成 SCRIPT_NAME),导致 PHP 找不到脚本文件security.limit_extensions 默认只认 .php,如果你用 .php5 或其他后缀,要手动加进去tail -f /var/log/apache2/error.log 或 journalctl -u php8.1-fpm -f,看到 “Primary script unknown” 就是路径问题,“No input file specified” 多半是 SCRIPT_FILENAME 错了别只看网页 404,有些情况 rewrite 规则其实在跑,只是逻辑写错了。用命令行直击底层:
apache2ctl -M | grep rewrite(输出应含
rewrite_module (shared))nginx -t && nginx -s reload,然后在
server 块加 rewrite_log on; error_log /var/log/nginx/rewrite.log notice;(注意权限)

curl -I http://localhost/about,若返回
HTTP/1.1 404 Not Found 且无 X-Powered-By: PHP,说明请求根本没进 PHPAllowOverride、nginx 里漏了 try_files、或日志没开导致瞎猜。rewrite 本身不难,难在它藏在 Web 服务器层,而错误表现却像 PHP 应用挂了。