重寫 URL

適用於:所有 API 管理 層

rewrite-uri 原則會將要求 URL 從公用格式轉換成 Web 服務所需的格式,如以下範例中所示。

  • 公用 URL - http://api.example.com/storenumber/ordernumber

  • 要求 URL - http://api.example.com/v2/US/hardware/storenumber&ordernumber?City&State

應該將一般人及/或瀏覽器可理解的 URL 轉換成 Web 服務所需的 URL 格式時,可使用此原則。 只有在公開替代 URL 格式時需要套用此原則;例如,簡潔 URL、RESTful URL、使用者易記 URL 或符合 SEO 的 URL 等單純的結構式 URL 格式,不含查詢字串,只包含資源的路徑 (在配置和授權後面)。 這樣做通常是基於美觀、實用性或搜尋引擎最佳化 (SEO) 目的。

注意

請依照原則陳述式中提供的順序,來設定原則的元素和子元素。 深入了解如何設定或編輯 APIM 原則

原則陳述式

<rewrite-uri template="uri template" copy-unmatched-params="true | false" />

屬性

名字 描述 是必要欄位 預設
template 含有任何查詢字串參數的實際 Web 服務 URL。 允許使用原則運算式。 使用運算式時,整個值必須是運算式。 Yes N/A
copy-unmatched-params 指定當連入要求中查詢參數不存在於原始 URL 範本時,是否要將其新增到重寫範本所定義的 URL。 允許使用原則運算式。 No true

使用方式

使用注意事項

使用此原則只能加入查詢字串參數。 無法在重寫 URL 中新增額外的範本路徑參數。

範例

<policies>
    <inbound>
        <base />
        <rewrite-uri template="/v2/US/hardware/{storenumber}&{ordernumber}?City=city&State=state" />
    </inbound>
    <outbound>
        <base />
    </outbound>
</policies>
<!-- Assuming incoming request is /get?a=b&c=d and operation template is set to /get?a={b} -->
<policies>
    <inbound>
        <base />
        <rewrite-uri template="/put" />
    </inbound>
    <outbound>
        <base />
    </outbound>
</policies>
<!-- Resulting URL will be /put?c=d -->
<!-- Assuming incoming request is /get?a=b&c=d and operation template is set to /get?a={b} -->
<policies>
    <inbound>
        <base />
        <rewrite-uri template="/put" copy-unmatched-params="false" />
    </inbound>
    <outbound>
        <base />
    </outbound>
</policies>
<!-- Resulting URL will be /put -->

如需使用原則的詳細資訊,請參閱: