Skip to content

Commit f32b585

Browse files
authored
Merge pull request #309227 from dlepow/rew
[APIM] Examples in rewrite-uri policy
2 parents b194011 + 33f637d commit f32b585

1 file changed

Lines changed: 38 additions & 10 deletions

File tree

articles/api-management/rewrite-uri-policy.md

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@ ms.author: danlep
1414

1515
[!INCLUDE [api-management-availability-all-tiers](../../includes/api-management-availability-all-tiers.md)]
1616

17-
The `rewrite-uri` policy converts a request URL from its public form to the form expected by the web service, as shown in the following example.
17+
The `rewrite-uri` policy converts a request URL from its public form to the form expected by the web service.
1818

19-
- Public URL - `http://api.example.com/storenumber/ordernumber`
20-
21-
- Request URL - `http://api.example.com/v2/US/hardware/storenumber&ordernumber?City&State`
22-
23-
This policy can be used when a human and/or browser-friendly URL should be transformed into the URL format expected by the web service. This policy only needs to be applied when exposing an alternative URL format, such as clean URLs, RESTful URLs, user-friendly URLs or SEO-friendly URLs that are purely structural URLs that don't contain a query string and instead contain only the path of the resource (after the scheme and the authority). This is often done for aesthetic, usability, or search engine optimization (SEO) purposes.
19+
Use this policy when you need to transform a human-friendly or browser-friendly URL into the URL format expected by the web service. Apply this policy only when exposing an alternative URL format, such as clean URLs, RESTful URLs, user-friendly URLs, or SEO-friendly URLs that are purely structural and don't contain a query string but instead contain only the path of the resource (after the scheme and the authority). You often make this change for aesthetic, usability, or search engine optimization (SEO) purposes.
2420

2521
[!INCLUDE [api-management-policy-generic-alert](../../includes/api-management-policy-generic-alert.md)]
2622

@@ -36,7 +32,7 @@ This policy can be used when a human and/or browser-friendly URL should be trans
3632
|Name|Description|Required|Default|
3733
|----------|-----------------|--------------|-------------|
3834
|template|The actual web service URL with any query string parameters. Policy expressions are allowed. When expressions are used, the whole value must be an expression. |Yes|N/A|
39-
|copy-unmatched-params|Specifies whether query parameters in the incoming request not present in the original URL template are added to the URL defined by the rewrite template. Policy expressions are allowed.|No|`true`|
35+
|copy-unmatched-params|Specifies whether query parameters in the incoming request that aren't present in the original URL template are added to the URL defined by the rewrite template. Policy expressions are allowed.|No|`true`|
4036

4137
## Usage
4238

@@ -46,21 +42,34 @@ This policy can be used when a human and/or browser-friendly URL should be trans
4642

4743
### Usage notes
4844

49-
You can only add query string parameters using the policy. You can't add extra template path parameters in the rewrite URL.
45+
You can only add query string parameters by using the policy. You can't add extra template path parameters in the rewritten URL.
46+
47+
## Examples
48+
49+
### Example 1: Basic URL rewrite
50+
51+
In the following example, the public URL is rewritten to match the backend service URL format, and query parameters are included based on other logic.
5052

51-
## Example
53+
- Public URL - `http://api.example.com/storenumber/ordernumber`
54+
55+
- Request URL - `http://api.example.com/v2/US/hardware/storenumber/ordernumber?City&State`
5256

5357
```xml
5458
<policies>
5559
<inbound>
5660
<base />
57-
<rewrite-uri template="/v2/US/hardware/{storenumber}&{ordernumber}?City=city&State=state" />
61+
<rewrite-uri template="/v2/US/hardware/{storenumber}/{ordernumber}?City=city&State=state" />
5862
</inbound>
5963
<outbound>
6064
<base />
6165
</outbound>
6266
</policies>
6367
```
68+
69+
### Example 2: Copy unmatched parameters
70+
71+
In the following example, the public URL is rewritten to match the backend service URL format, and the policy copies any unmatched query parameters to the new URL.
72+
6473
```xml
6574
<!-- Assuming incoming request is /get?a=b&c=d and operation template is set to /get?a={b} -->
6675
<policies>
@@ -73,7 +82,12 @@ You can only add query string parameters using the policy. You can't add extra t
7382
</outbound>
7483
</policies>
7584
<!-- Resulting URL will be /put?c=d -->
85+
7686
```
87+
### Example 3: Don't copy unmatched parameters
88+
89+
In the following example, the public URL is rewritten to match the backend service URL format, and the policy drops any unmatched query parameters.
90+
7791
```xml
7892
<!-- Assuming incoming request is /get?a=b&c=d and operation template is set to /get?a={b} -->
7993
<policies>
@@ -88,6 +102,20 @@ You can only add query string parameters using the policy. You can't add extra t
88102
<!-- Resulting URL will be /put -->
89103
```
90104

105+
### Example 4: Use policy expression in the template
106+
107+
In the following example, the policy uses expressions in the template to construct the request to the backend.
108+
109+
```xml
110+
<policies>
111+
<inbound>
112+
<base />
113+
<set-variable name="apiVersion" value="/v3" />
114+
<rewrite-uri template="@("/api" + context.Variables["apiVersion"] + context.Request.Url.Path)" />
115+
</inbound>
116+
</policies>
117+
118+
```
91119
## Related policies
92120

93121
- [Transformation](api-management-policies.md#transformation)

0 commit comments

Comments
 (0)