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

.htaccess 遷移至 IIS httpd.ini 完整轉換規則對照表

編輯:關於伺服器

Apache 遷移 IIS 時,.htaccess 重寫規則需轉換為 ISAPI_Rewrite 相容的 httpd.ini 格式。本文提供實戰轉換對照與核心差異解析。

核心語法差異

項目Apache (.htaccess)IIS (httpd.ini)
模組宣告RewriteEngine On[ISAPI_Rewrite]
路徑基準RewriteBase /(全域)每條規則前綴 /(相對站點根目錄)
模式匹配^pattern$(相對當前目錄)/pattern(從站點根目錄)
反向引用$1, %1$1(同)
標誌[R=301,L,NC]不支援標誌,需拆分規則或用 IIS 內建重導向

實戰轉換範例

原 .htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L,NC]
RewriteBase /
RewriteRule ^([^-/._-]+)\.html$ $1.php
RewriteRule ^([^-/._-]+)-op([^/]+)-sor([^/]+)-det([^/]+)\.html$ $1.php?op=$2&sor=$3&det=$4
...(更多規則)

轉換後 httpd.ini

[ISAPI_Rewrite]
# 域名標準化(需 IIS 內建 HTTP 重導向模組配合)
RewriteRule /([^-/._-]+)\.html$ /$1.php
RewriteRule /([^-/._-]+)-op([^/]+)-sor([^/]+)-det([^/]+)\.html$ /$1.php?op=$2&sor=$3&det=$4
RewriteRule /([^-/._-]+)-op([^/]+)-sor([^/]+)\.html$ /$1.php?op=$2&sor=$3
RewriteRule /([^-/._-]+)-sor([^/]+)\.html$ /$1.php?sor=$2
RewriteRule /([^-/._-]+)-op([^/]+)\.html$ /$1.php?op=$2
RewriteRule /([^-/._-]+)-page([^/]+)\.html$ /$1.php?page=$2
RewriteRule /([^-/._-]+)-op([^/]+)-sor([^/]+)-page([^/]+)\.html$ /$1.php?op=$2&sor=$3&page=$4

關鍵轉換點總結

  1. 移除 RewriteEngine On、RewriteBase、RewriteCond(ISAPI_Rewrite 不支援條件判斷)
  2. 所有模式前加 /,移除開頭 ^
  3. 域名強制跳轉改用 IIS 「HTTP 重導向」功能或網站綁定多主機頭 + Canonical 重導向
  4. 大小寫不敏感預設開啟,無需 NC
  5. 測試工具:IIS Manager → ISAPI_Rewrite → Test Pattern

遷移建議

  • 優先考慮應用層路由(MVC 框架內建),減少對 Web Server 重寫依賴
  • 複雜條件判斷(如 User-Agent、Cookie、GeoIP)建議移至應用代碼
  • 完整測試所有 URL 模式後再切換生產流量
Copyright © Windows教程網 All Rights Reserved