在 Ubuntu 上通过 Apache2 开启 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>
如果你用的是其他目录,改成对应路径即可。
保存后重启 Apache:
sudo systemctl restart apache2
sudo nano /etc/apache2/apache2.conf
找到:
<Directory /var/www/>
AllowOverride None
改为:
AllowOverride All
然后重启 Apache。
在网站根目录创建 .htaccess:
sudo nano /var/www/html/.htaccess
示例内容(隐藏 index.php):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
rewrite 模块已启用AllowOverride All.htaccess 在正确目录apache2ctl -M | grep rewrite
看到 rewrite_module 即成功。
如果你是用 Laravel / ThinkPHP / WordPress 等框架,我也可以给你对应的具体配置。