IIS 7.5 內建 URL Rewrite 模組,透過 web.config 即可實現各類 PHP/.NET 應用的偽靜態規則,無需修改伺服器層級設定。本文提供標準模板與主流 CMS(ShopEx、Discuz! X2/X2.5/X3)的現成規則。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!-- 規則放置於此 -->
</rules>
</rewrite>
</system.webServer>
</configuration>
記得在應用程式後台啟用偽靜態功能。
<match url="..." />:正則比對請求 URL 路徑(不含查詢字串)。<conditions>:附加條件,如排除實體檔案/目錄(IsFile、IsDirectory)、特定副檔名等。<action type="Rewrite" url="..." />:內部重寫至目標路徑,保留查詢字串可用 appendQueryString="true"。<action type="CustomResponse" statusCode="403" />:直接回應錯誤碼,常用於封鎖敏感路徑。<rule name="disableDownloadThemeHtml">
<match url="^themes/.*.(html|xml)$" ignoreCase="false" />
<action type="CustomResponse" url="/" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
</rule>
<rule name="urlRewrite">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" pattern=".(html|xml|json|htm|php|phtml|asp|aspx|jsp|cfm|pl|cgi|shtml)$" ignoreCase="false" />
<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}" appendQueryString="false" />
</rule>
<rule name="portal_topic">
<match url="^(.*/)*topic-(.+).html?*(.*)$" />
<action type="Rewrite" url="{R:1}/portal.php?mod=topic&topic={R:2}&{R:3}" />
</rule>
<rule name="portal_article">
<match url="^(.*/)*article-([0-9]+)-([0-9]+).html?*(.*)$" />
<action type="Rewrite" url="{R:1}/portal.php?mod=view&aid={R:2}&page={R:3}&{R:4}" />
</rule>
<rule name="forum_forumdisplay">
<match url="^(.*/)*forum-(\w+)-([0-9]+).html?*(.*)$" />
<action type="Rewrite" url="{R:1}/forum.php?mod=forumdisplay&fid={R:2}&page={R:3}&{R:4}" />
</rule>
<rule name="forum_viewthread">
<match url="^(.*/)*thread-([0-9]+)-([0-9]+)-([0-9]+).html?*(.*)$" />
<action type="Rewrite" url="{R:1}/forum.php?mod=viewthread&tid={R:2}&extra=page={R:4}&page={R:3}&{R:5}" />
</rule>
<rule name="group_group">
<match url="^(.*/)*group-([0-9]+)-([0-9]+).html?*(.*)$" />
<action type="Rewrite" url="{R:1}/forum.php?mod=group&fid={R:2}&page={R:3}&{R:4}" />
</rule>
<rule name="home_space">
<match url="^(.*/)*space-(username|uid)-(.+).html?*(.*)$" />
<action type="Rewrite" url="{R:1}/home.php?mod=space&{R:2}={R:3}&{R:4}" />
</rule>
<rule name="home_blog">
<match url="^(.*/)*blog-([0-9]+)-([0-9]+).html?*(.*)$" />
<action type="Rewrite" url="{R:1}/home.php?mod=space&uid={R:2}&do=blog&id={R:3}&{R:4}" />
</rule>
<rule name="forum_archiver">
<match url="^(.*/)*(fid|tid)-([0-9]+).html?*(.*)$" />
<action type="Rewrite" url="{R:1}/index.php?action={R:2}&value={R:3}&{R:4}" />
</rule>
<!-- X2.5/X3 專用插件規則 -->
<rule name="plugin">
<match url="^(.*/)*([a-z]+[a-z0-9_]*)-([a-z0-9_-]+).html?*(.*)$" />
<action type="Rewrite" url="{R:1}/plugin.php?id={R:2}:{R:3}&{R:4}" />
</rule>
{R:0}、{R:1} 等回參照捕獲群組內容。