在 Debian 上开启 Apache2 的 URL 重写(Rewrite)功能,通常按以下步骤操作:
Apache2 的 rewrite 功能由 rewrite 模块提供。
sudo a2enmod rewrite
执行后会提示模块已启用,需要重启 Apache:
sudo systemctl restart apache2
默认情况下,Debian 的 Apache 可能不允许在目录中使用 .htaccess 重写规则。
通常位于:
/etc/apache2/sites-available/000-default.conf
或你自己的虚拟主机配置文件。
在 <VirtualHost *:80> 中添加或修改:
<Directory /var/www/html>
AllowOverride All
</Directory>
如果是其他目录,把
/var/www/html改成你的网站根目录
确保 Directory 配置类似:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
sudo systemctl restart apache2
在网站根目录创建 .htaccess:
RewriteEngine On
RewriteRule ^test$ index.php [L]
访问:
http://你的IP/test
应重写到 index.php
rewrite 模块AllowOverride 不是 Allapache2ctl -M | grep rewrite
如果你是用 LNMP / 面板 / Docker / 特定框架(Laravel / WordPress),可以告诉我具体环境,我可以给你更精确的配置。