在 Ubuntu + ThinkPHP 环境下设置伪静态,通常取决于你使用的 Web 服务器(Apache 或 Nginx) 以及 ThinkPHP 版本(5.x / 6.x / 8.x)。下面分别说明。
sudo a2enmod rewrite
sudo systemctl restart apache2
编辑站点配置(示例):
sudo nano /etc/apache2/sites-enabled/000-default.conf
在 <VirtualHost> 中添加:
<Directory /var/www/html>
AllowOverride All
</Directory>
重启 Apache:
sudo systemctl restart apache2
.htaccess(通常是 public/.htaccess)
<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>
✅ 访问示例:
http://域名/index/index/hello
sudo nano /etc/nginx/sites-enabled/default
server {
listen 80;
server_name yourdomain.com;
root /var/www/html/public;
index index.php index.html;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
⚠️ ThinkPHP 6/8 推荐指向
public目录
sudo nginx -t
sudo systemctl reload nginx
config/app.php
'url_html_suffix' => '',
✅ 404
✅ 首页正常,其他页面 404
AllowOverride 未开✅ 出现 index.php 无法隐藏
如果你告诉我:
我可以给你精确到行的配置。