在CentOS上配置Apache2以支持SEO友好URL(也稱為“干凈URL”或“漂亮URL”)通常涉及以下幾個步驟:
啟用必要的模塊:
確保Apache的mod_rewrite
模塊已啟用。這個模塊允許你重寫URL。
sudo systemctl enable httpd
sudo systemctl start httpd
sudo systemctl status httpd
檢查mod_rewrite
模塊是否已加載:
sudo apachectl -M | grep rewrite
如果沒有加載,可以使用以下命令啟用它:
sudo systemctl restart httpd
配置.htaccess
文件:
在你的網站根目錄下創建或編輯.htaccess
文件。這個文件用于定義URL重寫規則。
sudo nano /var/www/html/.htaccess
添加以下內容到.htaccess
文件中:
<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虛擬主機:
確保你的Apache虛擬主機配置允許使用.htaccess
文件。編輯你的虛擬主機配置文件,通常位于/etc/httpd/conf/httpd.conf
或/etc/httpd/conf.d/
目錄下。
sudo nano /etc/httpd/conf/httpd.conf
找到<Directory>
部分,并確保允許使用.htaccess
文件:
<Directory "/var/www/html">
AllowOverride All
Require all granted
</Directory>
保存并退出編輯器,然后重啟Apache服務:
sudo systemctl restart httpd
測試配置:
確保你的網站現在可以正確處理SEO友好URL。嘗試訪問一個不存在的頁面,看看是否被重定向到index.php
。
通過以上步驟,你應該能夠在CentOS上配置Apache2以支持SEO友好URL。如果你使用的是其他Web服務器(如Nginx),配置步驟會有所不同。