在 CentOS 系统中,Apache 服务器使用 mod_rewrite 模块来实现伪静态规则。首先确保已经启用了 mod_rewrite 模块,如果没有启用,请运行以下命令启用:
sudo systemctl enable httpd
sudo systemctl start httpd
sudo systemctl status httpd
接下来,在 Apache 的配置文件(通常位于 /etc/httpd/conf/httpd.conf
或 /etc/httpd/conf.d/
目录下的某个文件)中添加以下内容:
<Directory "/var/www/html">
AllowOverride All
</Directory>
这将允许 .htaccess 文件覆盖目录级别的配置。
现在,在你的网站根目录(例如:/var/www/html
)下创建或编辑 .htaccess 文件,添加你需要的伪静态规则。以下是一些常见的伪静态规则示例:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
controller.php?action=home
):RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)$ controller.php?action=$1&method=$2 [L]
example.com/about-us
重写为 example.com/about-us.php
):RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
根据你的需求修改和添加相应的伪静态规则。保存 .htaccess 文件后,重启 Apache 服务器以使更改生效:
sudo systemctl restart httpd
现在,你的 CentOS 系统中的 Apache 服务器应该已经成功应用了伪静态规则。