0

I am in the process of re-writing an old website that was written using a number of different non microsoft technologies. The new site is asp.net web forms.

I have a list of OLD url's in a database and have a routine that will inspect the requested page url against that list of url's in the database, and perform the appropriate 301 redirect to the contents NEW url.

The question I have though, from an efficiency point of view is, how can I perform this OLD url lookup ONLY if the requested Url is essentially a 404/not found. I don't want to fire my 'Check-Old-Urls-And-Redirect-If-Found' routine with every single page request (via Application_BeginRequest in global.asax), as the majority of the time, the requested page will be a valid new .net page that doesn't requires a 301 redirect. Therefore I would like to be able to fire my routine ONLY if it's essentially about to throw a 404 error.

My routine to check the Db for old site Urls is cached, so at least it's not hitting the database every time, but I'd just like to prevent it from even calling this routine (and occasionally hitting the DB when the cache has expired) when it's not necessary.

I was thinking that perhaps I could perform the old url lookup from inside the Application_Error routine in global.asax, checking the error code for 404 status and in that scenario, do the old url lookup, but when testing, it doesn't appear to be hitting my breakpoint in that Application_Error routine in global.asax - instead the browser is showing the IIS 7 404 Not Found error.

It's imperative that the browser receives a 301 response to the new url and doesn't get an incorrect 404 response code (unless the requested url was neither a new or old flavour url).

Can anyone recommend an efficient approach to handling this situation?

marcusstarnes
  • 6,393
  • 14
  • 65
  • 112
  • Create a custom 404 page and redirect if needed from there. – Mike Miller May 14 '12 at 09:28
  • Is there a reason why you can't just let IIS deal with this? Then any old links are issued a 301 redirect before it even hits your site - much less of a performance hit. – Kevin Main May 14 '12 at 10:27
  • There is no consistent patterns to the old urls - they are an absolute mess, so I can't tell IIS what type of url/patterns to look out for. – marcusstarnes May 14 '12 at 10:37

1 Answers1

0

IMO best thing to do is install IIS Rewrite Module and create redirects for all old URL-s. You can have 301 redirects in separate XML file, it's a more cleaner approach, here is a SO post about this:

IIS7 urlrewrite module - Rules in external xml file

Update: You can simply put a list of url's, something like this :

    <rule name="Redirect map bluesunhotels">
      <match url=".*"/>
      <conditions>
        <add input="{bluesunhotels:{REQUEST_URI}}" pattern="(.+)"/>
      </conditions>
      <action type="Redirect" url="{C:1}" appendQueryString="false"/>
    </rule>
  </rules>
  <rewriteMaps>
    <rewriteMap name="bluesunhotels">
      <add key="/index.php?show=2326" value="/resort-afrodita.aspx"/>
      <add key="/index.php?show=2271" value="/starigrad-paklenica.aspx"/>
    </rewriteMap>
  </rewriteMaps>
Community
  • 1
  • 1
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
  • The only problem I can see with this for my situation is that I am relying on my asp.net project to generate the new urls/routes, so I don't want to be hard-coding redirect destination values, as there will be differences between the new url's on my development and production environments (with different id's etc being used throughout). – marcusstarnes May 14 '12 at 10:51
  • be aware that 301 are permanent redirects, and browsers cache it permanently, and maybe you can redirect it to single redirect page and from there redirect it again based on some code, Matt Cutts said that is ok to have two redirects http://www.youtube.com/watch?v=r1lVPrYoBkA – Antonio Bakula May 14 '12 at 11:11
  • @AntonioBakula can u answer me on this link, I have tried same as u said but issue is coming https://stackoverflow.com/questions/46651658/how-can-i-call-global-asax-on-asp-page-open –  Oct 09 '17 at 20:15