Search code examples
asp.net-mvchttp-redirecthttp-status-code-301

best way to redirect .cfm locations to new locations .net mvc 3 IIS 7.5


I am trying to determine the best way to redirect a set of files I have from an old site (coldfusion) to new locations on my new site (asp.net mvc 3).

I want to redirect these pages with a 301 status to let engines know that this is a permanent change.

Currently I have in my web.config a custom errors section set up to redirect any 404 to the home page, which works great for all of the old links that are no longer in service, but it's sending a 302 status which I don't want and it's sending all my redirects to home thereby not giving me the SEO that was getting from my old links.

I thought about just adding .cfm as a module mapping in IIS to my .net Isapi and creating all of my pages as cfm with a redirect by adding headers, but then realized that'd be a LOT of work...

is there another "easier" solution to achieve this?


Solution

  • Yes, HttpHandler. in your web.congig you register it to handle all *.cfm URLs ( http://msdn.microsoft.com/en-us/library/46c5ddfy%28v=vs.71%29.aspx ), and implementation will be just your 301 redirect. 10 lines of code at most.

    using System.Web;
    public class CfmHandler : IHttpHandler {
        public void ProcessRequest(HttpContext context) {
            // redirect here, you have HttpContext ready    
        }
        public bool IsReusable {
            // To enable pooling, return true here.
            // This keeps the handler in memory.
            get { return true; }  
        }
    }
    

    More at : http://msdn.microsoft.com/en-us/library/5c67a8bd%28v=vs.71%29.aspx