在 CentOS 系统中,配置 PHP 的 mod_rewrite 规则通常涉及以下几个步骤:
启用 Apache 的 mod_rewrite 模块:
首先,确保 Apache 的 mod_rewrite 模块已经启用。你可以使用以下命令来启用它:
sudo systemctl enable httpd
sudo systemctl start httpd
然后,启用 mod_rewrite 模块:
sudo a2enmod rewrite
最后,重启 Apache 服务以应用更改:
sudo systemctl restart httpd
配置 .htaccess 文件:
在你的网站根目录下创建或编辑 .htaccess 文件。这个文件用于定义 URL 重写规则。以下是一个简单的示例:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>
这个示例规则的意思是:
index.php 文件,则直接访问该文件。index.php。配置 Apache 虚拟主机:
如果你使用的是虚拟主机,确保在虚拟主机配置文件中允许使用 .htaccess 文件。编辑虚拟主机配置文件(通常位于 /etc/httpd/conf.d/ 或 /etc/httpd/conf.modules.d/ 目录下),添加或修改以下配置:
<Directory "/var/www/html">
    AllowOverride All
</Directory>
这个配置允许在 /var/www/html 目录下的网站使用 .htaccess 文件。
重启 Apache 服务: 最后,重启 Apache 服务以应用所有更改:
sudo systemctl restart httpd
通过以上步骤,你应该能够在 CentOS 系统中成功配置 PHP 的 mod_rewrite 规则。如果你遇到任何问题,请检查 Apache 的错误日志以获取更多信息。错误日志通常位于 /var/log/httpd/error_log。