Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

IIS 7+ web.config URL Rewrite 偽靜態完整配置指南:規則語法、參數映射與 IIS 6 遷移對照

編輯:關於伺服器

IIS 7+ 原生 URL Rewrite 模組

無需額外安裝 ISAPI 擴展,直接在 web.config <system.webServer> 區段聲明規則。

基礎規則結構


  
    
      
        
        
          
        
        
      
    
     ... 
  

關鍵屬性解析

  • name:規則唯一標識,**不可重複**。
  • match url:正則模式匹配 **相對 URL 路徑** (不含查詢字串)。
  • ignoreCase:大小寫敏感度。
  • action type:
    • Rewrite:內部重寫 (URL 不變,伺服器內部映射)。
    • Redirect:外部重導向 (301/302,瀏覽器地址列改變)。
  • url="Substitution":目標路徑,支援後向參考 {R:0} (完整匹配)、{R:1} (第一個捕獲組) 等。
  • appendQueryString:是否保留原查詢字串 (預設 true)。

實戰範例:文章頁偽靜態

目標:/articles/abc.html → 內部重寫至 /articles/Default.aspx?SubClass=abc


  
  

多參數映射對照 (IIS 6 vs IIS 7+)

場景IIS 6 (URLRewriter.dll)IIS 7+ (原生)
捕獲組引用$1, $2{R:1}, {R:2}
查詢字串連接& (需寫 &amp;)& (直接寫,XML 編碼自動處理)
條件判斷無 (需寫多條規則)<conditions> 支援 {HTTP_HOST}, {REQUEST_URI}, {QUERY_STRING} 等

IIS 6 遷移參考 (URLRewriter.dll)


  
  



  
    
      ~/article/(\w+)/(\d{8})/(\w+).html
      ~/article/default.aspx?Sub=$1&amp;UrlName=$3
    
  

常見陷阱與最佳實踐

  • stopProcessing="true":匹配成功後停止後續規則評估,**提升效能、避免衝突**。
  • 避免無限循環:Rewrite 目標 URL 不要再次匹配同一規則 (可用 conditions 排除)。
  • 效能:規則數 < 50 影響微小;大量規則建議合併正則或使用 rewriteMaps。
  • 測試工具:IIS 管理器 → 「URL Rewrite」→ 「測試模式」輸入 URL 即時查看匹配結果。
Copyright © Windows教程網 All Rights Reserved