IIS Redirect (URL Rewrite) 網址重新導向指南書


  1. 說明
    1. HTTP TO HTTPS
    2. 重導網址並包含 Query String
    3. 重導網址 Domain
    4. 重導網址 SubFolder
    5. 重導網址改變 SubFolder & QueryString
    6. 重導網址改變順序
    7. 重導網址簡化網址 提升 SEO
    8. 重導網址簡化網址 提升 SEO 2
    9. 重導網址簡化網址 提升 SEO 3
  2. IIS Server Variables
  3. 相關連結

筆記 IIS 使用 URL Rewrite 進行網址重導的各種資訊日常應用方式。

logo

說明

HTTP TO HTTPS

IIS 設定 HTTP 導向 HTTPS (IIS Redirect Http to Http with Url Rewrite)

重導網址並包含 Query String

src: https://sdwh.dev?q=3
des: https://sdwh2.dev/app?q=3
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect from sdwh.dev to sdwh2.dev" stopProcessing="true">
          <match url="(.*)" />
          <action type="Redirect" url="https://sdwh2.dev/app/{R:1}" 
            appendQueryString="true" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
    </system.webServer>
</configuration>

重導網址 Domain

src: http://www.oldsite.com
des: http://www.newsite.com
<rewrite>
  <rules>
    <rule name="Redirect to New Site" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.oldsite\.com$" />
      </conditions>
      <action type="Redirect" url="http://www.newsite.com/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

重導網址 SubFolder

src: http://example.com/products
des: http://example.com/shop
<rewrite>
  <rules>
    <rule name="Redirect Products to Shop" stopProcessing="true">
      <match url="^products(/?)(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^example\.com$" />
      </conditions>
      <action type="Redirect" url="http://example.com/shop/{R:2}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

重導網址改變 SubFolder & QueryString

src: https://www.example.com/products/details.aspx?id=123
des: https://www.example.com/product/123
<rewrite>
  <rules>
    <rule name="Product Redirect" stopProcessing="true">
      <match url="^products/details\.aspx$" />
      <conditions>
        <add input="{QUERY_STRING}" pattern="id=(\d+)" />
      </conditions>
      <action type="Redirect" url="/product/{C:1}" appendQueryString="false" />
    </rule>
  </rules>
</rewrite>

重導網址改變順序

src: https://www.example.com/news/2021/06/article.html
des: https://www.example.com/news/article/2021/06
<rewrite>
  <rules>
    <rule name="News Redirect" stopProcessing="true">
      <match url="^news/(\d{4})/(\d{2})/([^/]+)\.html$" />
      <action type="Redirect" url="/news/{R:3}/{R:1}/{R:2}" appendQueryString="false" />
    </rule>
  </rules>
</rewrite>

重導網址簡化網址 提升 SEO

src: https://www.example.com/category.aspx?name=books&page=2
des: https://www.example.com/books/2
<rewrite>
  <rules>
    <rule name="Category Redirect" stopProcessing="true">
      <match url="^category\.aspx$" />
      <conditions>
        <add input="{QUERY_STRING}" pattern="name=(\w+)&amp;page=(\d+)" />
      </conditions>
      <action type="Redirect" url="/{C:1}/{C:2}" appendQueryString="false" />
    </rule>
  </rules>
</rewrite>

重導網址簡化網址 提升 SEO 2

src: https://www.example.com/blog/post?id=9876&title=my-blog-post
des: https://www.example.com/posts/my-blog-post
<rewrite>
  <rules>
    <rule name="Blog Post Redirect" stopProcessing="true">
      <match url="^blog/post$" />
      <conditions>
        <add input="{QUERY_STRING}" pattern="id=(\d+)&amp;title=([^&amp;]+)" />
      </conditions>
      <action type="Redirect" url="/posts/{C:2}" appendQueryString="false" />
    </rule>
  </rules>
</rewrite>

重導網址簡化網址 提升 SEO 3

src: https://www.example.com/events/2022/conference?id=abcd1234
des: https://www.example.com/conference/abcd1234
<rewrite>
  <rules>
    <rule name="Event Redirect" stopProcessing="true">
      <match url="^events/(\d{4})/conference$" />
      <conditions>
        <add input="{QUERY_STRING}" pattern="id=([a-zA-Z0-9]+)" />
      </conditions>
      <action type="Redirect" url="/conference/{C:1}" appendQueryString="false" />
    </rule>
  </rules>
</rewrite>

IIS Server Variables

Variable Usage
{R:0} 代表與整個模式相符的 URL。在重寫中用於重新定向或轉換 URL。
{R:1}、{R:2} 代表在模式中使用括號的子模式的結果。例如,在正則表達式中使用括號進行分組時,{R:1} 代表第一個子模式的結果,{R:2} 代表第二個子模式的結果,以此類推。常用於重寫 URL 時保留特定部分的值。
{R:n} 引用規則中的捕獲組。數字 n 表示第 n 個捕獲組。例如,{R:1} 表示規則中的第一個捕獲組。
{C:n} 條件中的捕獲組(Capture Group)。數字 n 表示第 n 個捕獲組。例如,{C:1} 表示條件中的第一個捕獲組。
{HTTP_HOST} 代表 HTTP 請求中的主機名部分(域名)。常用於根據不同的主機名進行轉址或重寫規則。
{HTTPS} 代表是否使用 HTTPS 協議進行連接,值為 “on” 或 “off”。可用於條件判斷以進行適當的轉址或重寫。
{QUERY_STRING} 代表 URL 中的查詢字串部分。可用於條件判斷或重寫 URL 時保留查詢參數。
{REQUEST_URI} 代表完整的請求 URI(不包括主機名部分)。常用於重寫 URL 時進行條件判斷或提取 URI 的一部分。
{URL} 代表相對於應用程序的 URL 部分。常用於重寫規則和轉址規則中,操作 URL 的路徑部分。
{REQUEST_FILENAME} 代表相對於磁碟上的實際文件路徑。可用於條件判斷,例如檢查文件是否存在或判斷檔案類型。
{REQUEST_METHOD} 代表請求的 HTTP 方法(GET、POST、PUT 等)。常用於根據不同的請求方法進行重寫或轉址。
{REMOTE_ADDR} 代表發起請求的客戶端的 IP 地址。可用於根據客戶端 IP 地址進行條件判斷或限制訪問。
{SERVER_PORT} 代表伺服器接聽的連接埠號碼。常用於根據不同的連接埠進行轉址或重寫規則。

https://learn.microsoft.com/en-us/iis/web-dev-reference/server-variables

相關連結

IIS 筆記整理

IIS 網頁伺服器的安全設定 (IIS Security Configuration)