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 內建重導向 |
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
...(更多規則)
[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
RewriteEngine On、RewriteBase、RewriteCond(ISAPI_Rewrite 不支援條件判斷)/,移除開頭 ^NC