在 Ubuntu 上启用 Apache2 的 rewrite(重写)模块 通常步骤如下:
sudo a2enmod rewrite
执行后会提示模块已启用。
sudo systemctl restart apache2
或(旧版本):
sudo service apache2 restart
默认情况下,Apache 可能不允许在目录中使用 .htaccess 的 rewrite 规则。
sudo nano /etc/apache2/sites-available/000-default.conf
(如果你用的是其他虚拟主机,编辑对应文件)
<VirtualHost> 中添加:<Directory /var/www/html>
AllowOverride All
</Directory>
如果是其他目录,替换
/var/www/html
sudo systemctl restart apache2
在网站根目录创建 .htaccess:
RewriteEngine On
RewriteRule ^test$ index.php [L]
访问:
http://你的IP/test
如果能正常访问 index.php,说明 rewrite 已生效。
AllowOverride All.htaccess 在正确目录如果你有具体的框架(如 Laravel / WordPress / ThinkPHP)或报错信息,可以发出来我帮你针对性配置。