ThinkPHP 8 在 Apache 下启用伪静态
ThinkPHP 8 在 Apache 下启用伪静态,需要配置 .htaccess
文件并确保 Apache 支持 URL 重写。以下是详细设置步骤:
1. 启用 Apache 重写模块
首先确保 Apache 的 mod_rewrite
模块已启用。编辑 Apache 配置文件(通常是 /etc/apache2/apache2.conf
或 /etc/httpd/httpd.conf
):
LoadModule rewrite_module modules/mod_rewrite.so
确保以下行未被注释:(多条)
AllowOverride All
2. 创建或修改 .htaccess
文件
在 ThinkPHP 项目的 public 目录下创建或编辑 .htaccess
文件,添加以下内容:
<IfModule mod_rewrite.c>Options +FollowSymlinks -MultiviewsRewriteEngine On# 禁止访问 .env 文件RewriteRule ^\.env$ - [F,L]# 重写规则RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
3. ThinkPHP 配置支持
确保 config/app.php
中 url_route_must
和 url_rewrite_on
配置正确:
// config/app.php
return [// ...其他配置'url_route_must' => false,'url_rewrite_on' => true,// ...
];
4. 重启 Apache 服务
配置完成后,重启 Apache 服务使设置生效:
# Ubuntu/Debian
sudo systemctl restart apache2# CentOS/RHEL
sudo systemctl restart httpd