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

IIS 7.5 透過 web.config 實現偽靜態:常見 CMS 規則完整範本

編輯:關於伺服器

IIS 7.5 原生支援 URL Rewrite 模組,透過站點根目錄 web.config 即可靈活定義重寫規則,無需修改伺服器全域配置。本文提供標準模板與 Shopex、Discuz! X2.0/X2.5 等熱門 CMS 現成規則,開箱即用。

基礎 web.config 模板

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!-- 此處放入具體 rule -->
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

前置條件: IIS 已安裝 URL Rewrite Module 2.0+,且網站後台已開啟偽靜態功能。

Shopex 規則

<rewrite>
  <rules>
    <rule name="disableDownloadThemeHtml" stopProcessing="true">
      <match url="^themes/.*.(html|xml)$" ignoreCase="false" />
      <action type="CustomResponse" 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|php2|php3|php4|php5|phtml|pwml|inc|asp|aspx|ascx|jsp|cfm|cfc|pl|cgi|shtml|shtm|phtm)$" ignoreCase="false" negate="true" />
        <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>
  </rules>
</rewrite>

Discuz! X2.0 規則

<rewrite>
  <rules>
    <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>
  </rules>
</rewrite>

Discuz! X2.5 規則 (含插件支援)

<rewrite>
  <rules>
    <!-- 同 X2.0 核心規則 -->
    <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>
  </rules>
</rewrite>

部署檢查清單

  1. 將完整 <rewrite> 區塊置入 web.config 的 <system.webServer> 內。
  2. 確認網站實體路徑具備 IIS_IUSRS 讀取權限。
  3. 瀏覽器測試靜態化 URL,觀察是否正確映射至動態腳本。
  4. 如出現 500 錯誤,檢查 web.config XML 格式 (常見漏閉合標籤、特殊字元未轉義)。

善用 web.config 版控管理重寫規則,實現「配置即代碼」,遷移部署零成本。

Copyright © Windows教程網 All Rights Reserved