Search code examples
regexweb-configurlrewriter

Using Intelligencia UrlRewriter regex .asp pages to a holding page


When looking over the statistics for my site, I realized that the vast majority of traffic is coming via third party links to classic ASP pages which haven't existing for a few years now.

I decided that adding a bunch of urlMappings to the web.config wasn't a great idea, so I added Intelligencia UrlRewrite and tried to add a rule, as follows:

  <rewriter>
    <redirect url="^/(.*).asp$" to="~/pagenotfound.aspx?page=$1" />
  </rewriter>

The rule works, but it picks up any url which ends with .asp = such as /pagenotfound.aspx?page=someurl.asp.

Oops :)

I'm not exactly knowledgeable about regular expressions, how can I get it to ignore ".asp" which follows the question mark character?


Solution

  • Try this:

      <rewriter>
        <redirect url="^/([^?]*)\.asp(\?.*)?$" to="~/pagenotfound.aspx?page=$1" />
      </rewriter>
    

    That should make it ignore any URL which ends in .asp but contains a ? before it. The [^?] means "any character that's not a ?" instead of the * which means "any character".

    Edit: Added extra pattern to allow query strings after a .asp extension but not before them.