萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Apache Rewrite 規則配置全攻略:Discuz、SupeSite、ECShop 偽靜態實戰

URL 靜態化 (偽靜態) 能改善 SEO、美化連結、隱藏技術細節。Apache 透過 mod_rewrite 模組實現。本文詳解獨立主機與虛擬主機兩種場景的配置方法,並提供三大熱門 PHP 產品的完整規則集。

前置檢查:確認 mod_rewrite 已載入

  • Apache 1.x: httpd.conf 需有 LoadModule rewrite_module libexec/mod_rewrite.so 及 AddModule mod_rewrite.c
  • Apache 2.x: httpd.conf 需有 LoadModule rewrite_module modules/mod_rewrite.so
  • 若無,需重新編譯 Apache 加入 --enable-rewrite=shared 或動態載入模組。

場景一:獨立主機 (有 root 權限,修改 httpd.conf / VirtualHost)

原則: 規則必須寫在對應的 <VirtualHost> 區塊內,寫在外層可能不生效。修改後 必須重啟 Apache。

<IfModule mod_rewrite.c>
RewriteEngine On

# Discuz! 6.1.0 規則範例
RewriteRule ^(.*)/archiver/((fid|tid)-[\w-]+\.html)$ $1/archiver/index.php?$2
RewriteRule ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3
RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3
RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3
RewriteRule ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2
</IfModule>

場景二:虛擬主機/共享主機 (無 httpd.conf 權限,使用 .htaccess)

前提: 主機商必須支援 AllowOverride All (或至少 FileInfo) 並允許解析 .htaccess。請先諮詢確認。

  1. 在網站根目錄建立/編輯 .htaccess (Windows 下可從其他系統複製或下載空白檔)。
  2. 寫入規則,注意 RewriteBase 指向安裝子目錄 (根目錄則為 /)。
RewriteEngine On
RewriteBase /discuz   # 修改為你的實際安裝目錄,根目錄改為 /

# Discuz! 系統規則
RewriteRule ^archiver/((fid|tid)-[\w-]+\.html)$ archiver/index.php?$1
RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page%3D$3&page=$2
RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2
RewriteRule ^tag-(.+)\.html$ tag.php?name=$1

保存上傳後,進入論壇後台「搜尋引擎優化」開啟 URL 靜態化功能。

其他產品規則速查 (獨立主機格式,虛擬主機自行轉換 RewriteBase)

SupeSite / X-Space 6.0 UC 規則

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/([0-9]+)/spacelist(.+)$ /index.php?uid/$1/action/spacelist/type$2 [L]
RewriteRule ^/([0-9]+)/viewspace(.+)$ /index.php?uid/$1/action/viewspace/itemid$2 [L]
RewriteRule ^/([0-9]+)/viewbbs(.+)$ /index.php?uid/$1/action/viewbbs/tid$2 [L]
RewriteRule ^/([0-9]+)/(.*)$ /index.php?uid/$1/$2 [L]
RewriteRule ^/([0-9]+)$ /index.php?uid/$1 [L]
RewriteRule ^/action(.+)$ /index.php?action$1 [L]
RewriteRule ^/category(.+)$ /index.php?action/category/catid$1 [L]
RewriteRule ^/viewnews(.+)$ /index.php?action/viewnews/itemid$1 [L]
RewriteRule ^/viewthread(.+)$ /index.php?action/viewthread/tid$1 [L]
RewriteRule ^/mygroup(.+)$ /index.php?action/mygroup/gid$1 [L]
</IfModule>

ECShop 2.6.0 規則 (節選核心)

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)/index\.html$ $1/index.php
RewriteRule ^(.*)/category$ $1/index.php [L]
RewriteRule ^(.*)/feed-c([0-9]+)\.xml$ $1/feed.php?cat=$2 [L]
RewriteRule ^(.*)/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)-([0-9]+)-(.+)-([a-zA-Z]+)(.*).html$ $1/category.php?id=$2&brand=$3&price_min=$4&price_max=$5&filter_attr=$6&page=$7&sort=$8&order=$9
# ... 更多分類、商品、文章、品牌、標籤、搶購、團購規則請參考官方文檔
</IfModule>

除錯技巧

  • 開啟 Rewrite 日誌 (僅 Apache 2.2/2.4 舊版語法):
    RewriteLog "/var/log/apache2/rewrite.log"
    RewriteLogLevel 3 (1-9,生產環境建議關閉或設 1)。
  • Apache 2.4+ 使用通用日誌等級:
    LogLevel alert rewrite:trace3
  • 瀏覽器開發者工具 Network 面板觀察 301/302/404 狀態碼與重導向鏈路。
  • 規則測試工具:htaccess.madewithlove.be (線上測試)。

常見坑點

  1. RewriteBase 路徑錯誤導致規則不匹配。
  2. 規則順序錯誤:越具體的規則越要放前面 (如 category-...-min-max-attr... 應在 category-... 之前)。
  3. 未轉義特殊字元:正則中的 . 必須寫成 \.。
  4. .htaccess 權限問題:檔案需可被 Apache 進程讀取 (通常 644)。
copyright © 萬盛學電腦網 all rights reserved