在 Ubuntu 上启用 Apache 的 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改成你的网站根目录
保存后退出。
sudo nano /etc/apache2/apache2.conf
找到:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
改为:
AllowOverride All
sudo systemctl restart apache2
在网站根目录创建 .htaccess:
RewriteEngine On
RewriteRule ^test$ index.php [L]
访问:
http://your-domain/test
如果能正常访问 index.php,说明 rewrite 已生效。
rewriteAllowOverride 不是 All.htaccess 在网站根目录如果你用的是 Ubuntu + PHP + Laravel / WordPress,我也可以给你对应的 rewrite 示例。