在 Ubuntu 上设置 ThinkPHP 伪静态,通常取决于你使用的 Web 服务器(Apache 或 Nginx)。下面分别说明。
sudo a2enmod rewrite
sudo systemctl restart apache2
编辑你的站点配置(如 /etc/apache2/sites-enabled/000-default.conf):
<VirtualHost *:80>
DocumentRoot /var/www/html/public
<Directory /var/www/html/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
⚠️ 关键点是
AllowOverride All
.htaccess如果是 ThinkPHP 5 / 6,在 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>
sudo systemctl restart apache2
编辑 /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:/var/run/php/php8.1-fpm.sock;
}
}
ThinkPHP 6 推荐用
?s=方式
sudo nginx -t
sudo systemctl reload nginx
确保 config/app.php 中:
'url_rewrite' => true,
(ThinkPHP 6 默认已开启)
✅ 访问不了?
AllowOverride Allroot 是否指向 public✅ 出现 No input file specified
?s=如果你能告诉我:
我可以给你 精确配置。