在 Apache2 中配置“伪静态”(通常指用 .htaccess 实现 URL Rewrite),一般按下面步骤来。
sudo a2enmod rewrite
sudo systemctl restart apache2
Apache 默认可能不允许目录使用 .htaccess。
sudo nano /etc/apache2/apache2.conf
找到类似内容(如 /var/www/):
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
把:
AllowOverride None
改成:
AllowOverride All
保存后重启:
sudo systemctl restart apache2
在你的网站根目录(如 /var/www/html)创建或编辑:
nano /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?s=/$1 [QSA,L]
RewriteEngine On
RewriteRule ^article/([0-9]+)\.html$ article.php?id=$1 [L]
访问:
/article/123.html
实际指向:
/article.php?id=123
AllowOverride Allmod_rewrite 已启用.htaccess(前面有点)tail -f /var/log/apache2/error.log
在虚拟主机中配置:
<VirtualHost *:80>
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
</Directory>
</VirtualHost>
如果你告诉我 用的什么程序(WordPress / ThinkPHP / Laravel / 自定义),我可以直接给你一份可用的 .htaccess。