在Debian服務器上配置SEO友好的URL結構可以通過Apache的mod_rewrite模塊來實現。以下是詳細的步驟:
啟用mod_rewrite模塊: 打開終端并輸入以下命令來啟用mod_rewrite模塊:
sudo a2enmod rewrite
然后重啟Apache:
sudo systemctl restart apache2
配置.htaccess文件: 在Web服務器的根目錄(例如/var/www/html)中創建或編輯.htaccess文件。添加以下內容以啟用URL重寫引擎:
RewriteEngine on
設置基本的URL重寫規則: 假設你想將URL路徑“/article/12345”重寫為“/article.php?id=12345”,可以在.htaccess文件中添加以下規則:
RewriteRule ^article/([0-9]+)$ article.php?id=$1 [L]
這條規則會將所有以“/article/”開頭的請求重寫到“article.php”文件,并傳遞ID參數。
使規則生效: 確保Apache的配置允許使用.htaccess文件進行重寫。在Apache配置文件中(例如/etc/apache2/sites-available/000-default.conf),找到VirtualHost塊并添加以下內容:
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews AllowOverride All
Require all granted
</Directory>
然后重啟Apache:
sudo systemctl restart apache2
通過這些步驟,你可以配置Debian服務器上的Apache以生成SEO友好的URL結構。