在 Ubuntu 上让 Apache 开启 URL Rewrite(重写),一般按下面步骤来:
sudo a2enmod rewrite
然后重启 Apache:
sudo systemctl restart apache2
编辑配置文件(根据自己站点来):
sudo nano /etc/apache2/sites-available/000-default.conf
在 <VirtualHost *:80> 里加上:
<Directory /var/www/html>
AllowOverride All
</Directory>
⚠️ 如果是其他目录(如
/var/www/yourproject),改成对应路径
sudo nano /etc/apache2/apache2.conf
找到:
<Directory /var/www/>
AllowOverride None
改成:
AllowOverride All
在项目根目录(如 /var/www/html)创建或编辑:
sudo nano /var/www/html/.htaccess
示例(隐藏 index.php):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
sudo systemctl restart apache2
AllowOverride Allrewrite 模块已启用.htaccess 在正确目录apache2ctl -M | grep rewrite
如果你用的是 Laravel / ThinkPHP / WordPress,可以告诉我具体框架,我可以给你对应的 .htaccess 配置。