在 Ubuntu 上让 Apache 开启并使用 rewrite(重写)功能,通常按下面几步来:
sudo a2enmod rewrite
执行后会提示模块已启用,然后重启 Apache:
sudo systemctl restart apache2
Apache 2.4 默认对目录是 AllowOverride None,需要改成 AllowOverride All。
编辑你的站点配置文件(示例):
sudo nano /etc/apache2/sites-available/000-default.conf
在 <VirtualHost *:80> 里加上或修改:
<Directory /var/www/html>
AllowOverride All
</Directory>
如果你用的是其他目录(如 /var/www/example.com),对应修改即可。
保存后重启 Apache:
sudo systemctl restart apache2
在网站根目录创建或编辑 .htaccess:
RewriteEngine On
RewriteRule ^test$ index.php [L]
AllowOverride All 是否生效DocumentRoot 路径正确apache2ctl -M | grep rewrite
看到 rewrite_module (shared) 就说明成功了。
如果你用的是 Apache + PHP + Laravel / WordPress,我也可以给你对应具体的 rewrite 配置。