views:

582

answers:

2

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?

+1  A: 

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.

Amber
Dav,Thanks for the response. While this works, it means it no longer picks up .asp pages with a query string which is sort of defeating my purpose - I need to detect any legitmate .asp url and redirect, while not capturing .asp which happens to be in a query string.Thanksl
Heya Richard - you should be able to extend the url regex to add the potential for a query string after the .asp extension - I've edited my example above for something that I *believe* will work (not 100% sure if I nailed the syntax on it though).
Amber
I also added the backslash that should be in front of that first period (for .asp) - otherwise, it'll interpret it as a regular regex period which matches any character (instead of a literal period), which would result in it improperly matching something like /test.asp?asp
Amber
Chris Pebble
Amber
Good points :).
Chris Pebble
Dav/Chris,Thanks for the answers. The updated regex works great :) While originally my main goal was simply to record and redirect these invalid urls, pulling out the query string wasn't a bad idea either :)
A: 
<rewrite url="^~/browse/([^/.]+)\.aspx?$"
 to="~/browse-prints.aspx?dispCat=$1"/>

<rewrite url="^~/browse/([^/.]+)\.aspx(\?.*)?$"
 to="~/browse-prints.aspx$2&amp;dispCat=$1"/>
sankar