Dirt Simple 301 Redirect on Shared hosting and the 404 trick.

I’m using a shared hosting account for my web site. I have no control over the file types (extensions) that are handled by ASP.NET — only the standard file extensions (ASPX, ASMX, etc.) are supported. By request, I was asked to make my old RSS and Atom XML file feeds up to date with my new blog posts. During the transitions I’ve made, I may have lost some subscribers as I hadn’t kept them up to date as much as I would have liked.

So, with a wave of the magic ASP.NET wand and a little 404 magic, the two XML files are now serving up content from my new WordPress installation. On my web host, I can modify what pages handle 404 web server errors (file not found). I changed the setting to point to a new ASPX file, appropriately named, 404.aspx. Here’s what it contains:

<%@ Page Language="C#" %>
<%
    string qstr = HttpUtility.UrlDecode(Request.QueryString.ToString());

    if (qstr.Contains("index.xml") || qstr.Contains("atom.xml"))
    {
        string movedTo = "http://feeds.feedburner.com/wiredprairie";
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", movedTo);
        Response.End();

        return;
    }

    Response.Status = "404 Not Found";        
    Response.End();

    return;
%>

I grab the query string which minimally contains the requested Url, and if it’s either atom.xml or index.xml, I redirect to the FeedBurner location for the RSS feed. If it wasn’t one of those files, I just return a 404 error and quit (as I don’t have a custom 404 page developed).

Of course, with a bit more robust coding, this could handle a wider variety of cases and redirect to far more web pages. Or, you can just point to my RSS feed. :)