在 Ubuntu 下使用 Apache 开启伪静态(URL Rewrite),通常按以下步骤操作:
sudo a2enmod rewrite
开启后重启 Apache:
sudo systemctl restart apache2
编辑你的站点配置(示例):
sudo vim /etc/apache2/sites-available/000-default.conf
在 <VirtualHost *:80> 中添加或修改:
<Directory /var/www/html>
AllowOverride All
</Directory>
⚠️ 如果是其他目录(如 /var/www/yourproject),对应修改即可。
sudo vim /etc/apache2/apache2.conf
找到:
<Directory /var/www/>
AllowOverride None
改为:
AllowOverride All
然后重启 Apache:
sudo systemctl restart apache2
在项目根目录创建 .htaccess:
sudo vim /var/www/html/.htaccess
示例(通用伪静态):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
访问:
http://你的IP/任意非真实路径
若指向 index.php 且不 404,说明伪静态已开启 ✅
如果你用的是 Nginx、特定框架(Laravel / TP / WP),可以告诉我,我给你更精确的配置。