在 Ubuntu 下给 Apache 开启 伪静态(URL Rewrite),通常就是启用 mod_rewrite 并正确配置 .htaccess 或虚拟主机。下面是完整步骤:
sudo a2enmod rewrite
启用后重启 Apache:
sudo systemctl restart apache2
默认情况下,Apache 可能不允许 .htaccess 覆盖规则。
sudo nano /etc/apache2/sites-enabled/000-default.conf
(如果你用的是其他虚拟主机,请编辑对应文件)
<VirtualHost> 中添加或修改:<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
✅ /var/www/html 换成你的网站目录
sudo systemctl restart apache2
.htaccess 文件进入网站根目录:
cd /var/www/html
创建 .htaccess:
nano .htaccess
RewriteEngine On
index.php?page=xxx 伪静态为 /page/xxxRewriteRule ^page/([0-9]+)$ index.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
echo "<?php phpinfo();" > info.php
http://你的IP/info.php
mod_rewrite,确认已加载✅ 检查:
AllowOverride All 是否设置.htaccess 是否在正确目录.htaccess 开头(不是 htaccess.txt)Require all granted
检查 .htaccess 语法是否正确
sudo a2enmod rewrite
sudo nano /etc/apache2/sites-enabled/000-default.conf
# 添加 AllowOverride All
sudo systemctl restart apache2
如果你用的是 WordPress / ThinkPHP / Laravel / 宝塔 / Docker,可以告诉我,我可以给你对应环境的专用配置。