3

How can I setup a rule to replace & with & in url?

This works: www.../home.asp?h=1&w=2

This fails: www.../home.asp?h=1&w=2

Vixed
  • 3,429
  • 5
  • 37
  • 68
  • Could you provide more details on what you expect? What level do you expect to perform the rewrite? The first will have 2 url-encoded parametrs (h and w) while the second has a single parameter (h with value '1&w=2'). Both are reasonable by itself. So, maybe showing some more context might help answering the question. – rpy Apr 06 '16 at 20:42
  • @rpy I need the second one to work like the first one. – Vixed Apr 06 '16 at 20:50
  • Is this what some external (browser) sends in or what you recognize as input to the rewriting engine? What did you try? I'd guess the obvious: `&` by `&` and `&` by `&`? Aslo the semi-obvious `&` by `&`? Or some strange looking variants like `&` by `&`? (And what do you recognize as output from the rewriting? – rpy Apr 07 '16 at 06:43
  • Some error in google dev console and from some external browser. – Vixed Apr 07 '16 at 10:11

1 Answers1

1

For starters, it should be noted that you can access the messed up w parameter as follows:

Request.QueryString("amp;w");

However, I expect you would like something a little more eloquent :)

Assuming that you have access to the IIS URL Rewrite module, (IIS 7 and above), you can add some rules to web.config as follows:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="One Bad Ampersand" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{QUERY_STRING}" pattern="^([^&amp;]+)&amp;amp;([^&amp;]+)$" />
                    </conditions>
                    <action type="Rewrite" url="{R:1}?{C:1}&amp;{C:2}" appendQueryString="false" />
                </rule>
                <rule name="Two Bad Ampersand" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{QUERY_STRING}" pattern="^([^&amp;]+)&amp;amp;([^&amp;]+)&amp;amp;([^&amp;]+)$" />
                    </conditions>
                    <action type="Rewrite" url="{R:1}?{C:1}&amp;{C:2}&amp;{C:3}" appendQueryString="false" />
                </rule>
                <rule name="Three Bad Ampersand" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{QUERY_STRING}" pattern="^([^&amp;]+)&amp;amp;([^&amp;]+)&amp;amp;([^&amp;]+)&amp;amp;([^&amp;]+)$" />
                    </conditions>
                    <action type="Rewrite" url="{R:1}?{C:1}&amp;{C:2}&amp;{C:3}&amp;{C:4}" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

What these rules do is check for an incoming &amp; in the query string and replace it with &. I do not think it is possible to come up with a generic rule to handle an arbitrary number of occurrences. But, I have established a pattern for up to 3 occurrences that you should be able to follow to add as many as needed.

It should be noted that if you wish to redirect the user's browser, you may do so by changing the type attribute in each action from Rewrite to Redirect.

dana
  • 17,267
  • 6
  • 64
  • 88