APIM rewrite-uri Policy Functionality

Karl Gardner 195 Reputation points
2024-09-20T03:15:56.22+00:00

Hello,

Wondering what the rewrite-uri policy in apim actually does? The documentation says it "converts a request URL from its public form to the form expected by the web service". However, I thought that's what apim was already doing as it sends it to a backend api?

Thanks,

Karl

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,457 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. JananiRamesh-MSFT 29,276 Reputation points
    2024-09-20T04:14:18.1+00:00

    @Karl Gardner Thanks for reaching out. The rewrite-uri policy in APIM is more specific than the general routing that APIM performs.

    When you set up an API in APIM, you define a public endpoint that clients use to access your API. APIM then routes these requests to the appropriate backend service based on the configuration you provide.

    The rewrite-uri policy allows you to modify the request URL before it is sent to the backend service. This can be useful in several scenarios, such as path adjustments, versioning.

    For example, if your public API endpoint is https://api.example.com/v1/products but your backend service expects https://backend.example.com/api/products, you can use the rewrite-uri policy to adjust the URL accordingly.

    Here’s a simple example of how you might configure this policy:

    <inbound>
        <base />
        <rewrite-uri template="/api/products" />
    </inbound>
    

    This policy ensures that when a request comes to https://api.example.com/v1/products, it is rewritten to https://backend.example.com/api/products before being sent to the backend.

    hope this helps! do let me know incase of further queries, I would be happy to assist you.


  2. Sreeju Nair 12,666 Reputation points
    2024-09-22T10:12:42.73+00:00

    Hi Karl Gardner,

    APIM provides a layer of abstraction between the public API and the backend API. While APIM routes requests to the backend, it doesn’t inherently know how to modify the path unless you tell it. So, the rewrite-URI policy gives you fine-grained control to adjust the request path for compatibility with the backend, without changing how it looks to the client.

    You may read more about Rewrite API here

    https://learn.microsoft.com/en-us/azure/api-management/rewrite-uri-policy

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.