在 Ubuntu 上开启 Apache2 的 URL Rewrite(重写),通常步骤如下:
sudo a2enmod rewrite
启用后重启 Apache:
sudo systemctl restart apache2
编辑你的站点配置文件,例如:
sudo nano /etc/apache2/sites-available/000-default.conf
在 <VirtualHost *:80> 中添加或修改:
<Directory /var/www/html>
AllowOverride All
</Directory>
⚠️ 如果是其他目录,把
/var/www/html改成你的项目目录
保存后重启 Apache:
sudo systemctl restart apache2
在项目根目录创建或编辑 .htaccess:
RewriteEngine On
RewriteRule ^test$ index.php?page=test [L]
AllowOverride Allrewrite 模块已启用.htaccess 在正确目录查看是否启用模块:
apache2ctl -M | grep rewrite
DocumentRoot<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
如果你是用 Nginx + Apache、Docker 或某个具体框架(如 Laravel / WordPress),可以告诉我,我可以给你更精确的配置。