Search code examples
asp.nethttp-status-code-301httpmoduleresponse.redirectintercept

Response.Redirect Interception


We are using a CMS application developed in asp.net 4 with URL rewrite extension. Our application is hosting thousands of CMS pages and in solution Response.Redirect has been numerously used with URLs containing UPPER CASE letters. On using URL rewrite rule to convert requested URLs to lower case causing 301 status code for any page with Upper Case letter in its URL, which is not good for SEO perspective.

So I am looking forward a way to intercept those all Response.Redirect calls from a single location i.e. from global file, and convert them into lower case, rather then go in code files and convert all URLs pass to response.redirect method to lower case.


Solution

  • After getting into the MSDN and digging out Response.Redirect is setting location Header which does support get/set.

    void Application_EndRequest(object sender, EventArgs e)
    {
        HttpApplication application = sender as HttpApplication;
        HttpContext context = application.Context;
    
        if (context.Response.IsRequestBeingRedirected)
        {
            string redirectedLocation = context.Response.Headers["Location"];
            context.Response.Headers["Location"] = redirectedLocation.ToLowerInvariant();
        }
    
    }