How to rewrite a url without a query parameter doubling its value?

I need the following URL:

http://example.org/sessionstuff/kees/view.aspx?contentid=4&itemid=5

It needs to be rewritten so that it goes to:

http://example.org/sessionstuff/view.aspx?site=kees&contentid=4&itemid=5

Basically this will take a value keesand put it as a parameter site. I am using the IIS URL rewriter module that uses the rules in my web.config. I added the following code to my web.config:

<rule name="RedirectSite" stopProcessing="true">
    <match url="^(\D[^/]*)/(.*)$" />
    <action type="Rewrite" url="{R:2}?site={R:1}" />
</rule>

Everything works fine, but when I do the postback, the parameter sitedoubles. I checked this using the following code on my .aspx page:

<h3>Querystring</h3>
<ul>
    <% foreach (string key in Request.QueryString.Keys)
        Response.Write(String.Format("<li><label>{0}:</label>{1}</li>", key, Request.QueryString[key])); %>
</ul>
<asp:Button runat="server" Text="Postback" />

First time

Querystring
site: kees
contentid: 4
itemid: 5

Second time

Querystring
site: kees, kees <--- double
contentid: 4
itemid: 5

site ? .

: , appendQueryString="false" . >

+5
1

, , URL-, site= ( , ). , ?

9 : http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/

, , - :

<rule name="Query String Rewrite">
  <match url="^(\D[^/]*)/(.*)$" />
  <conditions>
    <add input="{QUERY_STRING}" pattern="^((?!site=).)*$" />
  </conditions>
  <action type="Rewrite" url="{R:2}?site={R:1}" />
</rule>

. , ^((?!site=).)*$ , โ€‹โ€‹ site=, , , . , .

, , : , site.

, , !

===

:

<rule name="RedirectSite" stopProcessing="true">
    <match url="^(\D[^/]*)/(.*)$" />
    <conditions>
        <add input="{QUERY_STRING}" pattern="^((?!site=).)*$" />
    </conditions>
    <action type="Rewrite" url="{R:2}?site={R:1}" />
</rule>


<rule name="RedirectSite2" stopProcessing="true">
    <match url="^(\D[^/]*)/(.*)$" />
    <conditions>
        <add input="{QUERY_STRING}" pattern="site=" />
    </conditions>
    <action type="Rewrite" url="{R:2}"  />
</rule>
+2

All Articles