在 CentOS 上配置 Apache 重寫規則,通常需要使用 mod_rewrite 模塊。以下是配置重寫規則的步驟:
sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd
mod_rewrite 模塊。運行以下命令啟用模塊:sudo systemctl enable --now httpd-mod_rewrite
/etc/httpd/conf/httpd.conf。如果需要為特定虛擬主機配置重寫規則,請找到相應的虛擬主機配置文件,例如 /etc/httpd/conf.d/yourdomain.conf。sudo vi /etc/httpd/conf/httpd.conf
或者
sudo vi /etc/httpd/conf.d/yourdomain.conf
<Directory> 部分,該部分定義了允許重寫規則的目錄。如果沒有這個部分,請添加一個。例如,要允許對網站根目錄應用重寫規則,請添加以下內容:<Directory "/var/www/html">
AllowOverride All
</Directory>
.htaccess 文件。例如,要為網站根目錄創建 .htaccess 文件,請運行以下命令:sudo vi /var/www/html/.htaccess
.htaccess 文件中添加重寫規則。以下是一個簡單的示例,將所有請求重定向到 index.php 文件:RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
保存并關閉 .htaccess 文件。
重新加載 Apache 服務以應用更改:
sudo systemctl reload httpd
現在,Apache 已根據您在 .htaccess 文件中定義的重寫規則處理請求。如果需要調試重寫規則,請查看 Apache 錯誤日志,通常位于 /var/log/httpd/error_log。