在 Apache2 中开启 rewrite(URL 重写)通常包含以下几个步骤,适用于 Ubuntu / Debian / CentOS 等常见系统。
sudo a2enmod rewrite
sudo systemctl restart apache2
通常 mod_rewrite 默认已编译,只需确保配置中存在:
LoadModule rewrite_module modules/mod_rewrite.so
然后重启:
sudo systemctl restart httpd
编辑你的虚拟主机文件(如 /etc/apache2/sites-enabled/000-default.conf):
<VirtualHost *:80>
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
然后重启 Apache:
sudo systemctl restart apache2
在网站根目录创建或编辑 .htaccess:
RewriteEngine On
RewriteRule ^test$ index.php [L]
⚠️ 注意:
必须设置 AllowOverride All,否则 .htaccess 不生效。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
apache2ctl -M | grep rewrite
如果你能提供:
我可以给你更精确的配置示例。