在 Ubuntu + PHP 环境下开启“伪静态”,通常取决于你使用的 Web 服务器(Apache 或 Nginx)。下面分别说明。
sudo a2enmod rewrite
sudo systemctl restart apache2
编辑你的站点配置(如 /etc/apache2/sites-enabled/000-default.conf):
<Directory /var/www/html>
AllowOverride All
</Directory>
然后重启:
sudo systemctl restart apache2
在网站根目录创建 .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>
✅ 这样就开启了伪静态。
编辑 /etc/nginx/sites-enabled/default 或你的站点配置:
server {
listen 80;
server_name example.com;
root /var/www/html;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
}
}
sudo nginx -t
sudo systemctl restart nginx
.htaccesslocation / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
}
}
Nginx:
try_files $uri $uri/ /index.php?$query_string;
浏览器访问:
http://你的域名/xxx
不再出现 index.php/xxx 即成功。
如果你能告诉我:
我可以给你精确配置。