1

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?

1 Answer 1

1

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>
6
  • Sorry - just one more question - I implemented the above rule, but in the browser - I was hoping the rule would mean that instead of having to enter http://localhost/article.aspx?id=342&title=some-article-title into the address bar, I could go direct to http://localhost/article/342/some-article-title but I receive a 404 error instead. Can I not use the rule for this purpose? Thanks
    – 4532066
    Apr 12, 2020 at 5:38
  • 1
    @4532066 Oh, it's the opposite. The "Match URL" field is the URL seen by the visitor (it's the URL as requested by the browser to the server), then, this "public" URL is rewritten and will be processed by the web-server as-is after the rewriting. So, the "Match URL" should be your "nice" URL and the rewritten URL is the "real" / "complex" URL that your webserver will understand
    – Swisstone
    Apr 12, 2020 at 9:41
  • Hi @Swisstone - thanks for your reply again. I'm sorry to sound slow here, but I don't understand what I need to do on the rule. Say I want the URL in the address bar to be page-44534.php but the "complex" URL behind that is actually page.php?id=44534, then how would the web.config rule look to achieve that? I tried setting match url to ^page-([0-9]+).phpand the action type to page.php\?id={R:1}but that didn't work either. Thanks
    – 4532066
    Apr 12, 2020 at 9:51
  • @4532066 you only need to escape the '?' in the "match" section. Don't escape the '?' in the 'Action' section, so, replace page.php\?id={R:1} with page.php?id={R:1}. I tested this rule and it works as expected.
    – Swisstone
    Apr 12, 2020 at 10:01
  • Hello again @Swisstone - thanks for your reply. I'm sorry I'm really confused now - if you have a moment, please can I ask if you would have time to paste in the web.config output for your rule, which allows a user to see e.g. page-44534.php in the browser, but where the underlying URL is actually page.php?id=44534? Thanks again
    – 4532066
    Apr 12, 2020 at 13:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .