首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

URLRewrite IIS6 首頁無法訪問:路由規則衝突與 httpHandlers 配置修正

     編輯:程式設計綜合問答

問題現象

ASP.NET 2.0 網站部署至 Windows 2003 + IIS6,使用 URLRewrite 模塊。首頁 (`~/`、`~/default.aspx`) 無法訪問,其他帶參數頁面正常。`httpHandlers` 配置 `path="*"` 導致首頁報「未將對象引用設置到對象的實例」;移除則動態頁面 404。

根本原因

  1. IIS6 處理管道差異:IIS6 僅支持 ISAPI 擴展,靜態文件、默認文檔由 IIS 原生處理,**不經過 ASP.NET 管道**。URLRewrite 作為 HttpModule 僅攔截進入 ASP.NET 的請求。
  2. 默認文檔未映射到 ASP.NET:請求 `~/` 由 IIS 內部重定向至 `default.aspx`,若 `default.aspx` 未在 IIS「默認內容頁面」列表,或未配置通配符映射,則不進入 ASP.NET,Rewrite 規則不生效。
  3. 規則順序與通配符衝突:`path="*"` 捕獲所有請求含靜態資源,導致 `DefaultHttpHandler` 處理失敗;移除後動態路由無處理器。

解決方案

方案一:配置 IIS6 通配符映射(推薦)

  1. IIS 管理器 → 網站屬性 → 主目錄 → 配置 → 應用程序映射 → 插入通配符映射:
    • 可執行文件:`C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll`
    • 取消勾選「驗證文件是否存在」
  2. 確保 `default.aspx` 在 IIS「文檔」標籤的「啟用默認內容頁面」列表中且排第一。

方案二:URLRewrite 規則調整(不修改 IIS 映射)

<rewriter>
  <!-- 首頁規則:精確匹配根路徑,重寫至 default.aspx -->
  <rewrite url="^~/?$" to="~/default.aspx" />
  <rewrite url="^~/default\.aspx$" to="~/default.aspx" />
  
  <!-- 動態頁面規則 -->
  <rewrite url="^~/([a-zA-Z0-9]+)/?$" to="~/123.aspx?p1=$1" />
  <rewrite url="^~/([a-zA-Z0-9]+)/(\d+)\.html$" to="~/123.aspx?p1=$1&p2=$2" />
</rewriter>

同時 `httpHandlers` 僅註冊 `.aspx`:

<httpHandlers>
  <add verb="*" path="*.aspx" type="URLRewriter.ModuleRewriter, URLRewriter" />
</httpHandlers>

方案三:升級至 IIS7+ 整合管道模式

Windows 2003/IIS6 已停止主流支持,強烈建議遷移至 Windows Server 2012 R2+ / IIS 8.5+,使用原生 URL Rewrite Module 2.0,配置 `` 徹底解決管道差異問題。

參考資源

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved