Apache 使用 .htaccess,IIS 7+ 原生使用 web.config。購買程式僅提供 .htaccess 時,無需花錢找人轉換,微軟官方 URL Rewrite Module 內建匯入功能,幾分鐘搞定。
.htaccess 檔案匯入完成後,網站根目錄會自動建立/更新 web.config,包含 <rewrite> 區段。範例:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
| 問題 | 原因 | 解決 |
|---|---|---|
| 匯入報錯「無法識別指令」 | .htaccess 含 Apache 專用指令 (如 Options, php_value) | 手動移除或改寫對應 IIS 設定 |
| 轉換後規則不生效 | 規則順序、匹配模式、條件判斷差異 | 逐條對照測試,善用「測試模式」驗證 |
| 中文 URL 亂碼 | 編碼不一致 | 確保 web.config 儲存為 UTF-8,IIS 設定 UTF-8 |
無法安裝模組時,直接聯繫空間商協助轉換或在控制台配置偽靜態。
URL Rewrite Module 讓 .htaccess → web.config 零成本遷移,站長必備技能。