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
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
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;([^&]+)$" />
</conditions>
<action type="Rewrite" url="{R:1}?{C:1}&{C:2}" appendQueryString="false" />
</rule>
<rule name="Two Bad Ampersand" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="^([^&]+)&amp;([^&]+)&amp;([^&]+)$" />
</conditions>
<action type="Rewrite" url="{R:1}?{C:1}&{C:2}&{C:3}" appendQueryString="false" />
</rule>
<rule name="Three Bad Ampersand" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="^([^&]+)&amp;([^&]+)&amp;([^&]+)&amp;([^&]+)$" />
</conditions>
<action type="Rewrite" url="{R:1}?{C:1}&{C:2}&{C:3}&{C:4}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
What these rules do is check for an incoming &
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
.