The Will Will Web

記載著 Will 在網路世界的學習心得與技術分享

分享幾個 IIS URL Rewrite 模組的設定技巧

我大概一年會設定個幾次 IIS 的 URL Rewrite 功能,對於不常設定的內容每次都還是要查詢一下範例,所以這裡就記錄一下幾個常用的設定技巧,以後就可以直接參考這篇文章!

URL

  • 設定首頁(而且也僅僅首頁)要轉向到特定網址 ( HTTP 302 暫時轉向 )

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="Home" stopProcessing="true">
              <match url="^$" />
              <action type="Redirect" url="https://www.duotify.com" redirectType="Found" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    
  • 設定首頁(而且也僅僅首頁)要轉向到特定網址 ( HTTP 301 永久轉向 )

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="Home" stopProcessing="true">
              <match url="^$" />
              <action type="Redirect" url="https://www.duotify.com" redirectType="Permanent" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    
  • 設定所有 HTTP 連線都會轉向到 HTTPS 加密連線

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="HTTP to HTTPS redirect" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTPS}" pattern="off" ignoreCase="true" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    
  • 設定 SPA (單一頁面應用程式) 網站的 URL Rewrite

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="SPA" stopProcessing="true">
              <match url=".*" />
              <action type="Rewrite" url="/index.html" />
              <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
              </conditions>
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    
  • 將特定網址 Rewrite 成另一個網址

    這裡我以常見的 robots.txt 檔案為例,將此連結轉向到 ASP.NET Core MVC 的 HomeController 中的 Robots 動作方法!

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="Robots" stopProcessing="true">
              <match url="robots.txt" />
              <action type="Rewrite" url="/Home/Robots" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    
  • www.domain.com 轉向到 domain.com 網址

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="Redirect to non-www" stopProcessing="true">
              <match url="^(.*)$" />
              <conditions>
                <add input="{HTTP_HOST}" pattern="^www\.domain\.com$" />
              </conditions>
              <action type="Redirect" url="https://domain.com/{R:1}" redirectType="Permanent" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    

你也有不錯的 URL Rewrite 設定範例嗎?歡迎留言給我!👍

相關連結

留言評論