API Management Best Practices, Tips, and Tricks #5 How to dynamically rewrite the URI

  • Sandro Pereira
  • Jun 4, 2024
  • 2 min read

Here we are, ready for another edition of API Management Best Practices, Tips, and Tricks. Today, we will address another helpful Best practice, Tip, and Trick to consider when implementing your API policies: dynamically rewriting the URI.

dynamically rewrite the URI

📝 One-Minute Brief

Rewriting backend URLs dynamically is a powerful technique in Azure API Management that enables flexible routing, abstraction, and versioning without changing backend services. In this article, part of the API Management Best Practices, Tips and Tricks series, you’ll learn how to dynamically rewrite URIs using API Management policies. The post explains common use cases, demonstrates how to manipulate incoming requests, and shows how dynamic URI rewriting improves maintainability, security, and scalability in real‑world API architectures.

#5 How to dynamically rewrite the URI?

Sometimes we need to dynamically set the backend URI because we are not only exposing the API on our API Management, but we are also modernizing and standardizing our API, providing a good naming convention and REST best practices.

In this exercise, we receive a query parameter as input in our operation. We want to retrieve that value and append it to the path to the backend URI path. For example:

  • /static-path/my-query-parameter

Well, the good news is that this is possible and quite simple to achieve. For that, we can use the rewrite-uri policy. The The rewrite-uri policy converts a request URL from its public form to the form expected by the web service.

Note: You can only add path parameters using the policy. You can’t add extra template query parameters in the rewrite-uri policy.

But first, it is important to understand how we can retrieve the query parameter, and for that, we can use the following expression (despite the fact that there are other ways to achieve that value):

context.Request.Url.Query.GetValueOrDefault("query-parameter-name", "default-value")

Now, to set up the rewrite-uri policy properly, you need to apply the equivalent policy statement that should look like this:

<rewrite-uri template="@{ 
	return "/static-path/"+context.Request.Url.Query.GetValueOrDefault("my-query-parameter", ""); 
}" /> 

I hope you enjoy this tip and stay tuned for the following Azure API Management Best practices, Tips, and Tricks.

If you liked the content or found it helpful and want to help me write more, you can buy (or help buy) my son a Star Wars Lego set! 

Buy me a Coffee
Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

Leave a Reply

Your email address will not be published. Required fields are marked *

The Ultimate Cloud
Management Platform for Azure

Supercharge your Azure Cost Saving

Learn More
Turbo360 Widget

Back to Top