URL 重寫 (URL Rewriting) 能將友善靜態化 URL (如 `/article/123.html`) 內部映射至動態頁面 (`/Article.aspx?id=123`),提升 SEO、隱藏技術細節、便於遷移。本文涵蓋 **Microsoft URLRewriter (舊專案)**、**IIS 7+ URL Rewrite Module (現代標準)**、**ASP.NET Core Middleware** 三種方案的完整配置與避坑指南。
下載 `Microsoft.Web.UrlRewriter.dll` (或原始碼專案編譯),加入專案參考。
<configuration>
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/article/(\d+)\.html</LookFor>
<SendTo>~/Article.aspx?id=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/category/([\w-]+)/?$</LookFor>
<SendTo>~/Category.aspx?name=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<system.web>
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
</system.web>
<system.webServer> <!-- IIS 7+ Integrated -->
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRewriter" type="URLRewriter.RewriterModule, URLRewriter" />
</modules>
<handlers>
<add name="HtmlRewriter" verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" preCondition="integratedMode" />
</handlers>
</system.webServer>
</configuration>
IIS 管理員 → 網站/虛擬目錄 → **配置** → **通配符應用程式對應** → 執行檔:`C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll`,**取消勾選「確認檔案是否存在」**。這讓 `.html` 請求也進入 ASP.NET 管線由 Rewriter 處理。
<rewrite>
<rules>
<rule name="ArticleHtml" stopProcessing="true">
<match url="^article/(\d+)\.html$" />
<action type="Rewrite" url="Article.aspx?id={R:1}" appendQueryString="true" />
</rule>
<rule name="CategorySeo" stopProcessing="true">
<match url="^category/([\w-]+)/?$" />
<action type="Rewrite" url="Category.aspx?name={R:1}" />
</rule>
<!-- 反向輸出重寫 (Response Filter) 讓頁面輸出的連結自動變靜態化 -->
<outboundRules>
<rule name="OutboundArticle" preCondition="IsHtml">
<match filterByTags="A" pattern="Article\.aspx\?id=(\d+)" />
<action type="Rewrite" value="article/{R:1}.html" />
</rule>
<preConditions>
<preCondition name="IsHtml">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rules>
</rewrite>
**優點**:原生 C++ 模組、效能極高、支援出站重寫 (自動轉換頁面連結)、正則功能豐富、GUI 管理介面。
// Program.cs / Startup.cs
app.UseRewriter(new RewriteOptions()
.AddRewrite(@"^article/(\d+)\.html$", "Article/$1", skipRemainingRules: true)
.AddRewrite(@"^category/([\w-]+)/?$", "Category/$1", skipRemainingRules: true)
.AddRedirect(@"^old-page\.aspx$", "new-page", 301)); // 永久重導向
// 或使用 MapWhen 精確控制
app.MapWhen(ctx => ctx.Request.Path.StartsWithSegments("/legacy"), appBuilder => {
appBuilder.UseRewriter(new RewriteOptions().AddRewrite(...));
});
| 模式 | 原理 | 優點 | 缺點 |
|---|---|---|---|
| URL 重寫 (偽靜態) | 動態頁面 + 重寫規則 | 即時資料、無生成延遲、部署簡單 | 每請求執行動態邏輯、資料庫壓力大 |
| 靜態化 (生成 .html) | 定時/觸發生成實體靜態檔 | 極速、零資料庫壓力、抗高併發 | 資料非即時、部署複雜、龐大檔案管理 |
| 真靜態 (CDN/SSG) | 建置時期生成 (Next.js, Astro, Hugo) | 極致效能、安全、邊緣緩存 | 動態互動需客戶端 JS / API |
選擇 **IIS URL Rewrite Module** 為首選 (效能、功能、維護最佳);舊專案受限於 IIS 6/傳統模式才用 Microsoft URLRewriter;新專案直接用 Core Middleware 或前端框架路由。