Handle 404 exceptions in EPiServer CMS

by: Ted Nyberg (Hallvarsson & Halvarsson)

As many of you know, there is a comprehensive 404 handler available for download through EPiCode. However, I just wanted a simple way of specifying a PageID and have that page act as a 404 response.

Easier said than done it seems

I actually got my 404 handler to work perfectly on my development machine - but that's because IIS 5 (the IIS version in Windows XP) allows wildcard mappings for all MIME types. IIS 6 (in Windows Server 2003) doesn't. Unless you modify the IIS metabase that is. I chose not to do so.

I'm halfway there

I urgently needed a decent 404 page which would display a pretty error message as well as a sitemap when an incorrect URL was requested. Most 404:s on the website were caused by requests for pages on an old replaced web site which didn't use friendly URL, so I could count on those requests to include the .aspx file extension - meaning they would be properly handled by IIS.

I'll do it myyyy  - waaaaaay

I've stripped down this code from the global.asax.cs file to prove the concept, so you may want to complement it:

        protected void Application_Error(object sender, EventArgs e)
        {
            //Get the last error that occured
            HttpException ex = 
(
HttpException)HttpContext.Current.Server.GetLastError(); string redirectUrl; //Get the HTTP code describing the type of error that occured switch (ex.GetHttpCode()) { case 404: //If a 404 error occured int.TryParse(
ConfigurationManager.AppSettings["404PageId"],
out redirectPageId); if (redirectPageId > 0) redirectUrl =
ToolBox.GetFriendlyURL(redirectPageId); break; case 500: //If a server exception occured int.TryParse(
ConfigurationManager.AppSettings["500PageId"],
out redirectPageId); if (redirectPageId > 0) redirectUrl =
ToolBox.GetFriendlyURL(redirectPageId); break; } if(redirectUrl!=null) //A redirect URL exists { Response.StatusCode = 301; Response.RedirectLocation = redirectUrl; Response.End(); Server.ClearError(); } }

Only triggered for ASP.NET requests

The Application_Error() method will execute properly for all requests handled by .NET - such as those for .aspx files. However, invalid friendly URL requests will not be handled by .NET and will therefore not trigger the error event.

I'm pretty sure there's a way to get this to work properly for invalid friendly URLs without running IIS 5, modifying the IIS 6 metabase or running IIS 7 on Windows Server 2008 - I just haven't found it yet... Any suggestions?

Update: Another solution is to use ASP.NET error handling. Check out my post on programmatically configuring customErrors to see how you could set error redirects to EPiServer pages.

07 February 2008


Comments

  1. There is one flaw in your solution though. Since your actually not sending back the 404 status code, search engines will index all pages as a perfectly good url. Instead set the response status to 404 (or 500) and do a Server.Transfer to your error page.
  2. Well, this is sort of a semantic discussion, but I believe 301 is the correct code. The old page has been "moved permanently", and the sitemap is used to find the new content. Besides, the 404 status code is almost always displayed with the built-in error message in browsers such as IE (is there a way around this?). This seems to happen even if a RedirectUrl has been specified. Search bots will no longer reference the old URL if they encounter a 301, instead the new URL will be indexed.
  3. Well, in that case you'll get a page that is indexed instead of all the incorrect linked pages to your site, and if a search bot happens to hit one of your pages and it returns a 500 error it is going to report back that this page has been permanently moved to the error page. And that's probably not the intention. All SEO "experts" I have talked to has said that it's essential to have a correct 404 page, but I guess that I don't have more that their word for it...
  4. Don't get me wrong, I'd definitely prefer to send a 404 status code in my response - but this seems to cause the browser to ignore the redirect URL and instead show its internal 404 message. Regarding the 500 error I agree - it should definitely not return a 301. But as I said, the code example has been abbreviated for clarity! :) I appreciate your input, Henrik!
  5. IE will show its internal error message if the length of the content in the returning page is lower than a specific threshold. See http://support.microsoft.com/kb/294807 and http://support.microsoft.com/kb/218155 for more information. Checking my registry shows that my IE require the 404 page to return more than 512 bytes of content.
  6. Awesome, Steve! I'll look into it right away! Thanks!
  7. Very nice solution. I implemented it and it works great - on my local test machine (win xp). On the server it doesn't work at all! (win 2003). In fact, it seems to never ever get to the Application_Error at all. Very strange. Would you have any clue how that could be? Any help appreciated! Thanks.
  8. Jonas, the solution above only works in IIS 6 (the Server 2003 version) when the request is for a file which has a file extension that is handled by ASP.NET. In other words it won't work for images, static HTML files or friendly URLs. This is because IIS 6 (in Server 2003) handles wildcard mappings differently than IIS 5 (in Windows XP). There are some ways around this, but I actually recommend that you look at my other post about programmatically configuring customErrors in web.config to achieve your goals. Good luck!
  9. I will absolutely have a look at your other post and try to apply that solution. But my greatest problem right now ist that IIS seems to ignore both what I write in the web.config and what I code in Global.asax. It works on my localhost but not on the server. In IIS I can change custom asp error but not to EPiServer... :(
Post a comment    
User verification Image for user verification  
Ted Nyberg (Hallvarsson & Halvarsson)

About me

I work for the corporate communications consultancy Hallvarsson & Halvarsson as a technical project manager and software architect.

Live Messenger

Hallvarsson & Halvarsson logo
 MCPD, MCTS and MCP logos

Bloggtoppen.se


Syndications


Archive


Tag cloud

EPiTrace logger