温馨提示×

如何开启debian apache2重写

小樊
51
2026-10-18 04:11:46
栏目: 智能运维

在 Debian 上开启 Apache2 的 URL 重写(Rewrite)功能,通常按以下步骤操作:


一、启用 rewrite 模块

Apache2 的 rewrite 功能由 rewrite 模块提供。

sudo a2enmod rewrite

执行后会提示模块已启用,需要重启 Apache:

sudo systemctl restart apache2

二、修改 Apache 配置,允许 .htaccess 重写

默认情况下,Debian 的 Apache 可能不允许在目录中使用 .htaccess 重写规则。

1. 编辑站点配置

通常位于:

/etc/apache2/sites-available/000-default.conf

或你自己的虚拟主机配置文件。

在 <VirtualHost *:80> 中添加或修改:

<Directory /var/www/html>
    AllowOverride All
</Directory>

如果是其他目录,把 /var/www/html 改成你的网站根目录


2. 如果是 Apache 2.4(Debian 8+)

确保 Directory 配置类似:

<Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

三、重启 Apache

sudo systemctl restart apache2

四、使用 .htaccess 测试重写

在网站根目录创建 .htaccess:

RewriteEngine On
RewriteRule ^test$ index.php [L]

访问:

http://你的IP/test

应重写到 index.php


常见问题

1. 页面 404 / 重写不生效

  • 没启用 rewrite 模块
  • AllowOverride 不是 All
  • 忘了重启 Apache

2. 查看已启用模块

apache2ctl -M | grep rewrite

如果你是用 LNMP / 面板 / Docker / 特定框架(Laravel / WordPress),可以告诉我具体环境,我可以给你更精确的配置。

0 踩