IIS URL Rewrite rule test pattern error: The input data to test does not match the pattern

iisrewrite

I am using this as a guide to work on an IIS Rewrite Rule:
https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

Sorry – also asked on SO (https://stackoverflow.com/questions/61149139/iis-url-rewrite-rule-test-pattern-error-the-input-data-to-test-does-not-match-t) but Server Fault might be a better fit for this type of question hence asking here.

This is my first rule, using the guide from the microsoft doc:

From: http://localhost/article/342/some-article-title
To: http://localhost/article.aspx?id=342&title=some-article-title

Enter into "Edit Inbound Rule"
Pattern: ^article/([0-9]+)/([_0-9a-z-]+)
Rewrite URL: article.aspx?id={R:1}&title={R:2}

Enter into "Test Pattern"
Input data to test: article/234/some-title
Pattern: ^article/([0-9]+)/([_0-9a-z-]+)
To: article.aspx?id=342&title=some-article-title

It works fine, and the test works fine too – screenshot here:

enter image description here

This is what the rule looks like in the web.config file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <directoryBrowse enabled="true" />
        <rewrite>
            <rules>
                <rule name="a">
                    <match url="^article/([0-9]+)/([_0-9a-z-]+)" />
                    <action type="Rewrite" url="article.aspx?id={R:1}&amp;title={R:2}" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

However, I want to have the rule work the other way around, so I tried the following in the rule definition:

From: http://localhost/article.aspx?id=342&title=some-article-title
To: http://localhost/article/342/some-article-title

Enter into "Edit Inbound Rule"
Pattern: ^article.aspx?id=([0-9]+)&title=([_0-9a-z-]+)
Rewrite URL: article/([0-9]+)/([_0-9a-z-]+)

Enter into "Test Pattern"
Input data to test: article.aspx?id=342&title=some-article-title
To: article/234/some-title

Unfortunately, that does not work – when I test the pattern, I get a "The input data to test does not match the pattern" response.

Screenshot of my attempt to get the rule to work the other way around:

enter image description here

This is what the 2nd rule looks like in the web.config file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <directoryBrowse enabled="true" />
        <rewrite>
            <rules>
                <rule name="a">
                    <match url="^article.aspx?id=([0-9]+)&amp;title=([_0-9a-z-]+)" />
                    <action type="Rewrite" url="article/([0-9]+)/([_0-9a-z-]+)" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

On the 2nd rule, I have tried with and without the "Append query string" option ticked (extract above is with the option ticked).

I wondered what I might be doing wrong?

Best Answer

It's because you must escape the ? like this: \? in the pattern.

Here is the correct version :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <directoryBrowse enabled="true" />
        <rewrite>
            <rules>
                <rule name="a">
                    <match url="^article.aspx\?id=([0-9]+)&amp;title=([_0-9a-z-]+)" />
                    <action type="Rewrite" url="article/([0-9]+)/([_0-9a-z-]+)" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>