Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS URL Rewriting - Friendly URL: Querystring variable is repeated

In my IIS 7 configuration, I have created friendly URLs to convert:

http://mysite/restaurant.aspx?Name=SomeName 

to

http://mysite/SomeName

To do this, I have the following rules:

<rule name="RedirectUserFriendlyURL1" enabled="true" stopProcessing="true">
  <match url="^Restaurant\.aspx$" />
    <conditions>
      <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
      <add input="{QUERY_STRING}" pattern="^Name=([^=&amp;]+)$" />
    </conditions>
  <action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>

<rule name="RewriteUserFriendlyURL1" enabled="true" stopProcessing="false">
  <match url="^([^/]+)/?$" />
    <conditions>
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      <add input="{REQUEST_FILENAME}" pattern=".aspx" negate="true" />
    </conditions>
  <action type="Rewrite" url="Restaurant.aspx?Name={R:1}" appendQueryString="false" />
</rule>
  1. Does the above seem correct to achieve what I'm trying?
  2. For some reason, on every postback I get:

    http://somesite/SomeName?Name=SomeName

Note that I have set appendQueryString to false.

like image 607
Donnie Thomas Avatar asked Dec 06 '25 00:12

Donnie Thomas


1 Answers

The form postback action uses the underlying url, not the raw url.

A simple solution (I believe the server side form action property is only available in 3.5+):

protected void Page_Load(object sender, EventArgs e)
{
    if ( !String.IsNullOrEmpty(Request.ServerVariables["HTTP_X_ORIGINAL_URL"]) )
    {
        form1.Action = Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
    }
}

http://blogs.iis.net/ruslany/archive/2008/10/23/asp-net-postbacks-and-url-rewriting.aspx

like image 182
ScottE Avatar answered Dec 08 '25 05:12

ScottE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!