0
votes

The site I'm working on (.Net 4.0 & VS2010 on IIS8) uses a single rule to rewrite all incoming URLs to .aspx pages existing in a single "/pages/" folder. For example, "www.site.com/hello" is rewritten to "/pages/hello.aspx".

The current rewrite rule is this:

    <rule name="pages" patternSyntax="ECMAScript" stopProcessing="true">
      <match url="^([a-z0-9_-]+(?:/[a-z0-9_-]+)*)/?$"/>
      <action type="Rewrite" url="/pages/{R:1}.aspx" appendQueryString="true" />
    </rule>

This is working fine, but I'm struggling to implement a custom 404 redirection for when either of the following are true:

a) The url format defined in the regex is not matched (i.e. the strict url format was not adhered to), or

b) The url regex is matched, but the rewrite to url="/pages/{R:1}.aspx" does not exist (e.g. the url is "/pages" but the physical file "/pages/hello.aspx" does not exist).

Can that be accomplished with rewite rules?

1

1 Answers

0
votes

Discovered the purpose of stopProcessing="false". :)

The rewritten url can be further tested in the next rule, to redirect to a 404 page if it does not end up pointing to an existing file. The working code is:

<rule name="pages" patternSyntax="ECMAScript" stopProcessing="false">
  <match url="^([a-z0-9_-]+(?:/[a-z0-9_-]+)*)/?$"/>
  <action type="Rewrite" url="/pages/{R:1}.aspx" appendQueryString="true" />
</rule>
<rule name="404" patternSyntax="ECMAScript" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="pages/Error404.aspx" />
</rule>