How to return 301/200 for selective 404 HTTP status or in other words say URL-rewriting

About a month ago, I came across an interesting request from my customer, who wanted to have render 301/200 for specific 404 pages. May be few of us are aware about URL re-writing & designing of HTTP modules; but interesting point to share here difference between 2 methods to achieve this

Issue Description:

  • Customer had a collection of URLs , which when we type in browser, it gives HTTP 404 Not found. The reason they are short handed URL and not the the complete URL
  • Example:
    1. https://anjalich11:5000/TI is the URL which actually needs to be redirected to : https://anjalich11:5000/TI/Pages/default.aspx
    2. Because of HTTP 404, Customer had issues with external sites which crawl these links. The crawler won’t crawl these links as it is rendered as HTTP 404
    3. We want these links to be rendered as HTTP 301 “Temporarily Moved” OR mark as HTTP 301 “Successful” which will help the crawler to crawl the links

Tool Used:

Points to NOTE :

Before we discuss about the solutions, there are few questions here: (Note: Read the 2 Scenarios and their respective fiddler test result for both the Scenarios.)

Scenario 1:

  1. What if we want the link to be crawled?
  2. What if we want the URL to be rendered as HTTP 200 (Success) instead of 404 (Page Not Found)
  3. What if we want the URL to render contents of other link but for User it seems to the same link.
  4. Example:

Scenario 2:

  1. What if we want the link to be marked as Temporarily Moved?
  2. What if we want the URL to be rendered as HTTP 301 (Temporarily Moved) instead of 404 (Page Not Found)
  3. What the redirection need to be mentioned dynamically rather than mentioning at IIS settings

Resolution / WorkAround

For Scenario 1: Taking Example to explain the resolution

  1. User enters link: https://anjalich11:4000/TI
  2. Data Rendered for the requested link is :https://anjalich11:4000/TI/Pages/default.aspx
  3. User still sees the entered URL & he wont know that URL is been rendered/processed from some other page

Resolution For Scenario 1:

  1. We need to create custom HTTPModule inheriting from IHttpModule for achieving URL Rewriting
    Note: We need to define 2 methods: Dispose() & Init()

  2. Depending upon the Authentication for SharePoint Site, we need to decide as which event need to be used for URL Rewriting

    • If you use Windows authentication with file authorization, we will need to change this so that URL rewriting is performed in either the BeginRequest or AuthenticateRequest events
    • Else we can do URL rewriting in Authorize event
  3. I will be selecting for Begin Event.

  4. Create an Event Handler for Begin event and register the event in the Initialize event

  5. Writing Code for URL Rewriting:

  6. Complete Code Snippet:

    namespace HTTPModule404To301 {
    public class HTTPModule404To301 : IHttpModule {

    public void Dispose() {}

    public void Init(HttpApplication context) {
    context.BeginRequest += new EventHandler(URLRewriter_BeginRequest);
    }

    void URLRewriter_BeginRequest(object sender, EventArgs e) {
    HttpApplication app = (HttpApplication)sender;
    Rewrite(app.Request.Path, app);
    }

    protected void Rewrite(string requestedPath,System.Web.HttpApplication app){
    Boolean blnRedirect ;

    // Business Logic to connect to data repository and thereby check if the shortcut for the link exists and get the link which need to be rendered
    if (blnRedirect)
    {
    // Here, as per our example mentioned above: strUrlToBeRendered= "/TI/Pages/default.aspx"
    // Note: It takes Relative URL Path.
    HttpContext.Current.RewritePath(strUrlToBeRendered, String.Empty, String.Empty);
    }
    }
    }
    }

  7. Test Result on FIDDLER TOOL

For Scenario 2: Taking Example to explain the resolution

  1. User enters link: https://anjalich11:4000/TI
  2. We need to render to Browser as HTTP 301 "Temporarily Moved" and thereby redirect to other URL Page showing Custom Message/the content
    Note: Make sure the redirected page exists

Resolution For Scenario 2:

  1. We need to create custom HTTPModule inheriting from IHttpModule for achieving URL Rewriting
    Note: We need to define 2 methods: Dispose() & Init()

  2. Depending upon the Authentication for SharePoint Site, we need to decide as which event need to be used for URL Rewriting

    • If you use Windows authentication with file authorization, we will need to change this so that URL rewriting is performed in either the BeginRequest or AuthenticateRequest events
    • Else we can do URL rewriting in Authorize event
  3. I will be selecting for Begin Event.

  4. Create an Event Handler for Begin event and register the event in the Initialize event

  5. Writing Code for URL Rewriting:

  6. Complete Code Snippet:

    namespace HTTPModule404To301 {
    public class HTTPModule404To301 : IHttpModule {

    public void Dispose() {}

    public void Init(HttpApplication context) {
    context.BeginRequest += new EventHandler(URLRewriter_BeginRequest);
    }

    void URLRewriter_BeginRequest(object sender, EventArgs e) {
    HttpApplication app = (HttpApplication)sender;
    Rewrite(app.Request.Path, app);
    }

    protected void Rewrite(string requestedPath,System.Web.HttpApplication app){
    Boolean blnRedirect ;

    // Business Logic to connect to data repository and thereby check if the shortcut for the link exists and get the link which need to be rendered
    if (blnRedirect)
    {
    //Adding Response header
    app.Response.StatusCode =301;
    app.Response.RedirectLocation= strRedirectLocation;
    app.Response.Flush();
    app.Response.End();
    }
    }
    }
    }

  7. Test Result on FIDDLER TOOL

    • When we enter https://anjalich11:4000/TI when there is no custom HTTP Module designed; we can see Value HTTP 404
    • After we designed custom HTTP Module, for URL: https://anjalich11:4000/TI, we can see Value HTTP 301 and then there will be another entry for the redirected Page with HTTP Status 200