You can read requested page in global.asax page and perform redirect from here base on some specific condition like case on requested page name.
Another option can be customize and intercept error in order to be able to manage error.
http://andynelms.com/2010/08/12/detecting-http-404-errors-in-global-asax/
--
You can start from this article:
http://www.codeproject.com/Articles/228879/Global-asax-in-ASP-NET
What you can do is implement only the Application_BeginRequest:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string currentUrl = HttpContext.Current.Request.Url.ToString().ToLower();
if (currentUrl.EndsWith("myOldPage.aspx"))
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", currentUrl.Replace("myOldPage.aspx", "myNewPage.aspx"));
Response.End();
}
}
See also this:
Global 301 redirection from domain to www.domain
It's possible to study appropriate replace pattern that use regex for accurate find and replace condition.
--
If you need to handle different file extension, I think that best approach could be:
an httpModule:
using System;
using System.Text.RegularExpressions;
using System.Web;
public class RedirectModule : IHttpModule
{
public RedirectModule()
{
}
public String ModuleName
{
get { return "RedirectModule"; }
}
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(this.context_BeginRequest);
}
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
//check here context.Request for using request object
if (context.Request.FilePath.ToLower().EndsWith("airconditioning.htm"))
{
string redirectUrl = "http://www.example.com" + Regex.Replace(context.Request.FilePath, "airconditioning.htm", "acrepair.html", RegexOptions.IgnoreCase);
context.Response.Redirect(redirectUrl);
}
}
void IHttpModule.Dispose() { }
}
You've to registed this in web.config in differnt way if not integrated pipeline or integrated pipeline
not integrated pipeline:
<system.web>
<httpModules>
<add name="RedirectModule" type="RedirectModule"/>
</httpModules>
</system.web>
integrated pipeline:
<system.webServer>
<modules>
<add name="RedirectModule" type="RedirectModule"/>
</modules>
</system.webServer>