Windows 主機 IIS 6/7 常使用 ISAPI_Rewrite (httpd.ini) 實現 URL 重寫,而非 Apache 的 .htaccess。本文說明 httpd.ini 基本語法與常見規則範例。
[ISAPI_Rewrite]
# 3600 = 1 hour
# CacheClockRate 3600
RepeatLimit 32
# 保護配置檔
RewriteRule ^/httpd(?:\.ini|\.parse\.errors).* [F,I,O]
# 重寫規則
RewriteRule /index.html /index.php
RewriteRule /new-([0-9]+).html$ /newxx\.php\?uid=$1
| 規則 | 說明 |
|---|---|
RewriteRule /index.html /index.php | 將 `/index.html` 內部重寫至 `/index.php`,瀏覽器網址列保持 `.html` |
RewriteRule /new-([0-9]+).html$ /newxx\.php\?uid=$1 | 捕獲數字 ID,重寫至動態頁帶參數。`$1` 為第一個捕獲組 |
RewriteRule 格式:`RewriteRule 模式 目標 [標誌]`;[L] 最後一條、[I] 忽略大小寫、[O] 允許覆蓋、[F] 禁止存取 (403);# 靜態化文章頁
RewriteRule /article-([0-9]+).html$ /article.php?id=$1
# 分頁
RewriteRule /list-([0-9]+)-([0-9]+).html$ /list.php?cat=$1&page=$2
# 目錄結構模擬
RewriteRule /category/([a-z0-9-]+)/$ /category.php?slug=$1
# 強制 www
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
# 強制 HTTPS (ISAPI_Rewrite 3.x)
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
web.config + URL Rewrite Module,功能更強、效能更好。掌握 httpd.ini 規則,即可在 IIS 環境實現靈活的 URL 靜態化與重導向。