在 Linux 系统下,通常使用 Apache 或 Nginx 作为 Web 服务器
Apache 的 mod_rewrite 规则:首先确保已启用了 mod_rewrite 模块。然后,在 .htaccess 文件或 httpd.conf 中添加以下内容:
RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ index.php/$1 [L]这个规则表示,如果请求的文件或目录不存在,将请求重写到 index.php 文件。
Nginx 的 rewrite 规则:在 Nginx 配置文件(通常是 /etc/nginx/sites-available/default 或 /etc/nginx/nginx.conf)中添加以下内容:
location / { try_files $uri $uri/ /index.php?$args;}这个规则表示,如果请求的文件或目录不存在,将请求重写到 index.php 文件。
注意:在修改配置文件后,需要重启 Web 服务器以使更改生效。对于 Apache,可以使用 sudo service apache2 restart(Ubuntu/Debian)或 sudo systemctl restart httpd(CentOS/RHEL)命令;对于 Nginx,可以使用 sudo service nginx restart(Ubuntu/Debian)或 sudo systemctl restart nginx(CentOS/RHEL)命令。


