I want IIS 10 to redirect requests to several other web apps (ASP.NET Core 6.0 and Angular 14)
Let's say I have Kestrel listening on http://localhost:5009/ serving one of those apps, I want to type
http://localhost/PassThrough/
into browser address and to have it redirected to
http://localhost:5171/
And also
http://localhost/PassThrough/todo/param1
to
http://localhost:5171/todo/param1
UPDATE:
Tried a rewrite URL method suggested by @TengFeiXie
and a bunch of variants. None of it worked. The problem with this method I think is IIS will only allow you to match a pattern after the first / and if you rewrite the matched part it will not add port in the correct place (before the first /). Redirect will also not redirect correctly for the same reason.
The second suggestion - reverse proxy also did not bare fruit: Both
<rewrite>
<rules>
<rule name="Reverse Proxy to webmail" stopProcessing="true">
<match url="^PassThrough/(.*)" />
<action type="Rewrite" url="http://localhost:5171/{R:1}" />
</rule>
</rules>
</rewrite>
and
<rewrite>
<rules>
<rule name="Reverse Proxy to webmail" stopProcessing="true">
<match url="http://localhost/PassThrough/(.*)" />
<action type="Rewrite" url="http://localhost:5171/{R:1}" />
</rule>
</rules>
</rewrite>
in web.config end up redirecting to
http://localhost/localhost:5171
instead of
http://localhost:5171
So same issue basically in both suggestions. It boils down to setting up the rule. If you are able to provide the rule that works let's say in xml form I'd be happy to test it.
Latest:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="PassThrough" stopProcessing="true">
<match url="http://localhost/PassThrough(/(.*))*" />
<action type="Redirect" url="http://localhost:5171{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
IIS provides official components, URL Redirection or Reverse Proxy both can meet your needs.
Get the URL Rewrite module: https://iis-umbraco.azurewebsites.net/downloads/microsoft/url-rewrite. You can refer to the steps in the official documentation to create redirect rules, redirect from http://localhost/App1/ to http://localhost:5009/.
Reverse Proxy needs Application Request Routing: https://iis-umbraco.azurewebsites.net/downloads/microsoft/application-request-routing.
Configuring Rules for the Reverse Proxy: https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing#configuring-rules-for-the-reverse-proxy.
The official documentation for URL redirection and reverse proxy has detailed steps and reference examples.
Update:
OK, It seems you are not familiar with rewrite rules. I'll demonstrate the steps to go from http://localhost/PassThrough/ to http://localhost:5171/. You'll find it's really easy.
I have an article.html on port 5174.
Then I added the following inbound rules in Default Web Site(port 80).
In Web.config
<rules>
<rule name="ReverseProxy" stopProcessing="true">
<match url="passthrough/(.*)" />
<action type="Rewrite" url="http://localhost:5171/{R:1}" />
</rule>
</rules>
Request: http://localhost/PassThrough/article.html. Successfully got the article.html.