Skip Links

Network World

Tyson Kopczynski

Creating a custom 404 page to handle link redirection for ASP.NET web applications

By tyson.kopczynski on Sun, 11/16/08 - 3:19am.
Newsletter Signup

For some time now, I've wanted to document how to setup a custom 404 page that can handle link redirection for an ASP.NET web application.  In my case, I just needed to customize the 404 behavior to handle custom redirects for a DotNetNuke site that I was setting up.  But, others might find these steps useful to handle broken links or other creative error handling behavior they want to build into their sites/applications.

Anyhow, to set this up the first step is to create your custom 404.aspx page.  In my case that page looked something like this:

<%@ Page Language="VB" runat="server" %>
<script language="vb" runat="server">
Sub Page_Load()
    Dim strURL, arpath, strFile
    strURL = UCase(request.servervariables("QUERY_STRING"))
    arPath = Split(strURL, "/")
    strFile = arPath(UBound(arPath,1))

    if InStr(strURL, UCase("oldpage1.htm")) <> 0 then
        response.redirect ("/Events/Default.aspx")
    elseif InStr(strURL, UCase("oldpage2.htm")) <> 0 then
        response.redirect ("/Publications/Default.aspx")
    elseif InStr(strURL, UCase("ic-folder")) <> 0 then
        response.redirect ("/Site/Files/" & strFile)
    else
        lblFileString.Text = strFile
    end if
End Sub
</script>
<html>
<head>
    <meta name="robots" content="noindex,nofollow">
    <title>mysite.com - 404 Page Not Found</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
    <UserControl:Header runat="server" />
    <h2>Page Not Found (404 Error)</h2>
    <asp:panel id="pnlValid404" runat="server">
        <p><asp:label id="lblFileString" runat="server" ForeColor="#FF0000" /> could not be located.<p>
    </asp:panel>
    <asp:panel id="pnlNotValid404" runat="server" Visible="False">
        <p>This page works only as a redirect when an ASPX page is not found.</p>
    </asp:panel>
</body>
</html>

Within the above code you will notice that the first part of the aspx page consists of some logic.  This logic is what determines where the requestor is redirected to when they encounter a 404 error.  For example, if you had a page that existed, but has since been removed and there still happens to be a bunch of marketing materials referencing that page.  You would add to the if,elseif,else,then statement a line that might look like:

    elseif InStr(strURL, UCase("oldmarketingpage2.htm")) <> 0 then

        response.redirect ("/NewMarketing/Default.aspx")

...

The same would also be true for structure changes where you needed to modify entire paths as shown with the ic-folder portion of the statement.  Then at the end of the statement is any logic you want to put in for actual 404 errors that were not intentional.  After all, at the end of the day, you still need to tell a web site visitor that a page, link, etc. is invalid.

After creating this page, the next step is to name it something like 404.aspx and dump it into a folder called something like ServerErrors.  Next, you then need to open your sites/applications web.config and turn on custom errors by modifying the customErrors tag as shown here:

   <customErrors mode="On">

       <error statusCode="404" redirect="/ServerErrors/404.aspx" />

   </customErrors>

Notice that for all 404 errors we are now redirecting to the 404.aspx page that was created in the previous two steps.  Lastly, you will also need to modify the IIS site properties so that your custom 404 page is used for all 404 errors.  To do this, open the Internet Services Manager.  Then on your site, right mouse click, select Properties, and then go to the Custom Errors tab.  Next, find the 404 HTTP Error, select it, and edit its properties.  In the error mappings properties dialog box change the message type to URL and set the URL to /ServerErrors/404.aspx.

Done... you now finished setting up your very own custom 404 page.

If you like this, check out some other posts from Tyson:

  • Microsoft Discontinues Support for Windows 3.X (as an embedded system)
  • Kyoto Temple Night View - Powered By Google!
  • My quest for SSH within PowerShell revisited!
  • How to read Certificates and CRLs using PowerShell

 

Or if you want, you can also check out some of Tyson's latest publications:

  • Windows PowerShell Unleashed (2nd Edition)
  • Windows Server 2008 Unleashed (Yes, I did help on this book)

 

Lastly, visit the Microsoft Subnet for more news, blogs, and opinions from around the Internet.  Or, sign up for the bi-weekly Microsoft newsletter.  (Click on News/Microsoft News Alert)

Welcome, visitor. Register Log in
About Hidden Microsoft

With more than ten years of experience in IT, Tyson Kopczynski has become a specialist in Active Directory, Information Assurance, Windows automation, PKI, and IT security practices. Tyson is also the founding author of the Windows PowerShell Unleashed series and has been a contributing author for such books as Microsoft Internet Security and Acceleration (ISA) Server 2006 Unleashed and Microsoft Windows Server 2008 Unleashed. He has also written many detailed technical papers and guides covering various technologies. As a consultant at Convergent Computing, Tyson works with and provides feedback for next generation Microsoft technologies since their inception and has also played a key role in expanding the automation and security practices at CCO. Tyson also holds such certifications as the Certified Information Systems Security Professional (CISSP), the SANS Security Essentials Certification (GSEC) and SANS Certified Incident Handler (GCIH), and the MCTS (Application Platform, Active Directory, and Network Infrastructure).


Certifications:

  • Certified Information Systems Security Professional (CISSP)
  • SANS Security Essentials Certification (GSEC)
  • SANS Certified Incident Handler (GCIH)
  • MCTS (Application Platform, Active Directory, and Network Infrastructure)
  • Microsoft Certified Systems Engineer (MCSE) Security
  • CompTIA Security+

Publications:


Other Stuff:

  • Blogger NetworkWorld.com from June 2007
  • GIAC Advisory Board from 2009
  • SANS GSEC Local Mentor (a long time ago)
  • CompTIA Security+ SME (a long time ago)
  • Judge, Imagine Cup 2005 Int'l IT Competition
  • Judge, Imagine Cup 2007 Int'l IT Competition