在 CentOS 上配置 PHP URL 重写规则,通常需要使用 Apache 或 Nginx 作为 Web 服务器。这里我将分别为 Apache 和 Nginx 提供配置方法。
对于 Apache:
mod_rewrite 模块。可以使用以下命令启用它:sudo systemctl enable httpd
sudo systemctl start httpd
sudo a2enmod rewrite
.htaccess 文件。例如,如果你的网站根目录是 /var/www/html,则在该目录下创建或编辑 .htaccess 文件:cd /var/www/html
nano .htaccess
.htaccess 文件中添加以下内容,根据需要进行修改:RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
这将把所有非文件和非目录的请求重写到 index.php。
.htaccess 文件,然后重启 Apache 服务以应用更改:sudo systemctl restart httpd
对于 Nginx:
/etc/nginx/nginx.conf 或 /etc/nginx/conf.d/default.conf。使用文本编辑器打开,例如:sudo nano /etc/nginx/conf.d/default.conf
server 块中,找到 location 部分,然后添加以下内容:location /
{
try_files $uri $uri/ /index.php?$args;
}
这将把所有非文件和非目录的请求重写到 index.php。
sudo systemctl restart nginx
现在,你已经在 CentOS 上为 PHP 配置了 URL 重写规则。请确保你的 PHP 应用程序能够正确处理这些重写后的请求。