Good Afternoon,
My question is quite lengthy and I will do my best to provide full details to present my issue clearly.
Task: I want to redirect certain urls to specific pages
Items:
1. I'm inserting the old urls into a db table and in this same table I'm associating the new path
2. I have logic in place to connect to the DB and query this table to retrieve the new path
3. A custom 404 error page
Problem:
Not all of the Urls are absolute, some are just relative (e.g. /InvalidLink.htm, /index.html ) and some just the path (e.g. /privatelabel, /socialsites). I have logic in my custom error page code behind to check to see if I have a redirect I can grab from the DB table and if not generate the 404 page. However, I'm not being redirected to the new paths, but instead receiving the 404. I am hitting the events in the code behind, but for some reason I'm not hitting the logic to lookup values in my redirect table.
Code for Code behind of 404:
public partial class error : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Url != null && !string.IsNullOrEmpty(Request.Url.Query))
{
int origUrlStartIndex = Request.Url.Query.IndexOf("http");
if (origUrlStartIndex >= 0)
{
string redirectUrl = Request.Url.Query.Substring(origUrlStartIndex);
Uri test = new Uri(redirectUrl);
string newLocation = SiteRedirects.GetRedirect(test.AbsolutePath);
if (!String.IsNullOrEmpty(newLocation))
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", newLocation);
Response.End();
}
}
}
}
}
Code for Redirect (locate in a seperate file:
public static string GetRedirect(string virtualPath)
{
log.Info("Looking for redirect: " + virtualPath);
string newPath = String.Empty;
if (redirects.ContainsKey(virtualPath.ToLower()))
{
newPath = redirects[virtualPath.ToLower()];
}
return newPath;
}
Question: What am I missing, why does this logic not work?