URI Redirects

Last post 07-03-2009 12:39 PM by RichardD. 1 replies.

Sort Posts:

  • URI Redirects

    07-01-2009, 4:04 PM
    • Member
      1 point Member
    • trunkmusic75
    • Member since 06-04-2009, 7:38 PM
    • Posts 5

    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?

    Filed under: ,
  • Re: URI Redirects

    07-03-2009, 12:39 PM
    Answer
    • Contributor
      2,776 point Contributor
    • RichardD
    • Member since 09-03-2002, 7:43 AM
    • Sussex, UK
    • Posts 364

    The error page set in the customErrors section of the web.config file will only apply to requests which are routed through the ASP.NET pipeline. If you want all 404 errors to use your page, you need to change the configuration via IIS Manager as well. In IIS7, you use the "Error Pages" feature; in IIS6 or earlier, you use the "Custom Errors" tab in the properties of the site.

Page 1 of 1 (2 items)