Home > Back-end >  Rewriting URL not working when using three segments
Rewriting URL not working when using three segments

Time:12-10

This code works perfectly to url rewrite 2 segments of the URL.

For example

/nottinghamshire/newark

However, if I add string t = Request.QueryString["t"].Replace("-", " ").ToLower().Replace(".aspx", "") to the mix, meaning

/nottinghamshire/newark/plumbers

It fails, and 404s like so: enter image description here

yet this works?

?r=nottinghamshire&c=newark&t=plumbers

This is my code

Web config:

  <rule name="rewritereview">
    <match url="^([^/] )/([^/] )?$" />
    <conditions>
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      <add input="{REQUEST_URI}" pattern="\.js|\.css|\.img|\.scimg" negate="true" />
    </conditions>
    <action type="Rewrite" url="/city.aspx?r={R:1}&amp;c={R:2}&amp;t={R:3}" appendQueryString="false" />
  </rule>

Code behind on city.aspx.cs

string r = Request.QueryString["r"].Replace("-", " ").ToLower();
string c = Request.QueryString["c"].Replace("-", " ").ToLower().Replace(".aspx","");
string t = Request.QueryString["t"].Replace("-", " ").ToLower().Replace(".aspx", "");

if (r != null && c != null && t != null)
{
  // populate page
}
else // 404?
{

}

What am I doing wrong?

CodePudding user response:

Try Updating the match URL. adding an extra "/([^/] )"

<match url="^([^/] )/([^/] )/([^/] )?$" />
  • Related