Redirecting Routes in Web Servers: Why, How, and Where to Do It.
Blog Author : Rahul Khedekar
In my last blog, I explained how to upgrade old .NET applications to the latest .NET frameworks. Apart from upgrading to the latest versions, our goal was to break down a monolith into smaller independent services. We started with 2 services and upgraded them to .NET Core 8. There were some breaking changes in the upgraded .NET frameworks, mainly related to AutoMapper, Entity Framework, and MySQL connectors. As these were breaking changes, we had to thoroughly test all new APIs. Unit test cases would have been probably right approach, but existing APIs had very little unit test coverage. So we started creating test data for all APIs. But the problem was that there were some APIs that were used by some other services and didn’t have any documentation about these APIs. Also, these APIs used to consume quite complex JSON payloads, which had a lot of variations. So we could not test all cases. We decided to deploy them on the QA environment so that we can test these APIs with actual data.
We deployed these new APIs as V2. We wanted to route all requests coming to the old APIs to V2 without asking other API consumers to make changes at their end. We wanted to control switching between V1 and V2. So we decided to use redirect routes from V1 to V2.
In this blog, we’ll explore:
Why Do We Need Route Redirection?
Here are some common scenarios:
How Web Browsers Handle Redirects
When a web browser receives an HTTP response with a redirection status code (like 301, 302, or 307), it automatically makes a follow-up request to the new location.
Key Redirect Status Codes:
301 : Moved Permanently
302 : Found (Temporary Redirect)
307 : Temporary Redirect
308 : Permanent Redirect
Redirecting in Different Web Servers
1. Apache (using .htaccess)
RewriteEngine On
RewriteRule ^old-url$ /new-url [R=301,L]
2. NGINX
location /old-path {
return 301 /new-path;
}
3. Node.js/Express
app.get('/old-path', (req, res) => {
res.redirect(301, '/new-path');
});
4. Options in IIS (Internet Information Services)
1. URL Rewrite Module
Used for both redirects and rewrites.
Redirect (client-side):
Option 1: URL Rewrite (Redirect)
Example (web.config):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!-- API 1: Redirect /api/v1/* to new API -->
<rule name="Redirect Samples to new API" stopProcessing="true">
<match url="^api/v1/(.*)" />
<action type="Redirect" url="https://example.com/api/v2/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
NOTE: URL Rewrite converts POST calls to GET. So if you are using POST calls, then ARR might be a better option.
Option 2: Reverse Proxy Using ARR (Application Request Routing)
Steps for using ARR:
Example (web.config):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!-- API 1: Redirect /api/v1/* to new API -->
<rule name="Redirect Samples to new API" stopProcessing="true">
<match url="^api/v1/(.*)" />
<action type="Rewrite" url="https://example.com/api/v2/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
With web.config, we can easily revert / change routes without re-deploying the application.
If you want to exclude a specific route from rerouting (don’t re-route auth routes) it can also be achieved using adding a condition like this
<rules>
<rule name="Redirect Samples to new API" stopProcessing="true">
<match url="^api/v1/(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/api/v1/auth$" negate="true" />
</conditions>
<action type="Rewrite" url="https://example.com/api/v2/{R:1}" />
</rule>
</rules>
Middleware-Based Redirects (ASP.NET Core)
When using ASP.NET Core or another framework, you can configure middleware for dynamic routing logic:
app.Use(async (context, next) =>
{
if (context.Request.Path.StartsWithSegments("/old"))
{
context.Response.Redirect("/new", permanent: true);
return;
}
await next();
});
This approach is suitable for:
Application-only redirects (not accessible at the IIS level)
Which Approach Should You Use with IIS?
Conclusion
Redirection is a powerful tool when used correctly. Whether you’re optimizing for SEO, migrating APIs, or setting up reverse proxies, choosing the right redirection method based on your server and use case is crucial.
On IIS, the combination of URL Rewrite and ARR offers a flexible and robust mechanism for handling both simple and complex routing requirements. For application-specific behaviour, use middleware within your app logic.
On IIS, the combination of URL Rewrite and ARR offers a flexible and robust mechanism for handling both simple and complex routing requirements. For application-specific behaviour, use middleware within your app logic.