在 Ubuntu 上用 Apache2 开启“伪静态”(通常是用 .htaccess + mod_rewrite 实现 URL 重写),一般按下面步骤来:
sudo a2enmod rewrite
sudo systemctl restart apache2
Apache 默认可能不允许 .htaccess 覆盖配置。
比如:
sudo nano /etc/apache2/sites-available/000-default.conf
或你的虚拟主机文件。
在 <VirtualHost *:80> 里加上:
<Directory /var/www/html>
AllowOverride All
</Directory>
/var/www/html改成你的网站根目录
sudo systemctl restart apache2
在网站根目录创建或编辑 .htaccess:
nano /var/www/html/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
.htaccess 不生效sudo chown -R www-data:www-data /var/www/html
tail -f /var/log/apache2/error.log
如果你用的是 ThinkPHP / Laravel / WordPress / 其他框架,可以告诉我,我给你对应的具体伪静态规则。