Rewrite module - Rule action not working with rewrite maps
#24,618 opened on Aug 6, 2020
Description
Describe the bug
Using rewrite maps in the URL attribute of a rewrite rule action doesn't seem to work. If we have the following rules and rewrite maps defined:
<?xml version="1.0" encoding="utf-16"?>
<rewrite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<rules>
<rule name="blog redirects" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{blog-map:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="query id redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{QUERY_STRING}" pattern="(.*)(\bid=([0-9]+-[0-9]{4}))(.*)" />
</conditions>
<action type="Redirect" url="{R:0}?{C:1}id={id-map:{C:3}}{C:4}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="blog-map" defaultValue="">
<add key="/somepage.html" value="/otherpage.html" />
</rewriteMap>
<rewriteMap name="id-map" defaultValue="">
<add key="1234-1234" value="ABCD-ABCD" />
</rewriteMap>
</rewriteMaps>
</rewrite>
The first rule works correctly and performs the redirect from somepage.html to otherpage.html.
However, the second rules doesn't seem to work and a request to index.html?id=1234-1234 doesn't redirect to the expected index.html?id=ABCD-ABVD.
Also, if the URL value of the second rule is changed to something "hardcoded" such as "bing.com", then the redirect action is performed which indicates the issue seems not to be related to the condition pattern but to the action URL.
To Reproduce
- Create a .net core web application (empty template)
- Add the Microsoft.NetCore.Rewrite package
- Create the rewrite file using the rules above
- Configure startup.cs to load the iis rewrite file
- Run the application and try with the requests mentioned on the description above
Exceptions (if any)
N/A
Further technical details
- ASP.NET Core version 2.1 (but it happens with 3.1 too)
- Visual Studio Enterprise 2019
▶ dotnet --info .NET SDK (reflecting any global.json): Version: 5.0.100-preview.4.20258.7 Commit: 65f0fc2cad
Runtime Environment: OS Name: Windows OS Version: 10.0.19042 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.100-preview.4.20258.7\
Host (useful for support): Version: 5.0.0-preview.4.20251.6 Commit: 47ec733ba7
.NET SDKs installed: 2.1.806 [C:\Program Files\dotnet\sdk] 3.1.300 [C:\Program Files\dotnet\sdk] 5.0.100-preview.4.20258.7 [C:\Program Files\dotnet\sdk]
.NET runtimes installed: Microsoft.AspNetCore.All 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.0-preview.4.20257.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.0-preview.4.20251.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.0-preview.4.20251.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]