A Custom 404 Page for Graffiti
Have you seen a 404 error on madprops.org? I hope not. But if you have, you would have noticed that it's not the default 404 page that your browser normally shows.
After re-reading Jeff Atwood's post about custom 404 errors, and seeing the great job Dave did on his when he migrated to BlogEngine.NET, I decided that this site, too, should have a custom 404 page with some helpful links on it, and I decided to use Graffiti's theme engine to do it.
First, I created a new, uncategorized post. I gave the post the title of "Not Found!" and switched over to the options and set its name to "404". For the body of the post, I wrote a brief spiel about how the page you were looking for isn't here anymore, and that you should try some other options - for example the lists of most recent and most popular posts.
Now I needed to get those links to appear on my page. Graffiti's Chalk theme engine allows for custom "views" based on the name of the post, so I created a new file called "404.view" which would be used to display just that one post.
My 404.view file, then, contains the standard markup for rendering a post (without the extra bits like when the post was created, what tags it has, or any kind of comments), along with a few extra bits underneath. First, to display the last 15 posts made to my site, I use this markup:
<h3>Recent Posts</h3> <ul>$macros.ULRecentPosts(15)</ul>
Then, to display the 15 most-viewed posts, I use this:
#foreach ($post in $data.PopularPosts(15)) #beforeall <ul> #each <li><a href="$post.Url">$post.Title</a></li> #afterall </ul> #end
You can see how powerful Graffiti's theme system is, making use of the NVelocity templating engine.
Lastly, I wanted to make sure that the 404 page didn't show my sidebar boxes, just for neatness. To accomplish this, I made a custom layout file called "404.layout.view", which excluded all the sidebar markup that I use on the default layout page. The "postname.layout.view" syntax means that Graffiti will use that file as the overall structure of the site for that post, rather than the default "layout.view" file.
The result, then, is a "Not Found!" page that hopefully guides the visitor if not to the page they were looking for, then at least to a page they'll be interested in reading.
# Trackback from Graffiti 404 Manager on 1/02/2009 1:54 PM
# Trackback from Graffiti 404 Manager : Scott Cate: Technology on 3/02/2009 4:21 AM
Comments
# Dave Burke
2/07/2008 2:19 PM
Aw, shucks. Thanks! Yeap, your custom 404 page works great. I added a "1" to this url and saw your recent and most popular post lists. I didn't see Atwood's post. He's definitely right, it's time we handle 404's (as cool guys like you and me are doing :-) Cheers!
# mabster
2/07/2008 2:33 PM
Yeah I didn't actually include a link to my 404 page, because Graffiti is logging every visit to it (since it's just a normal page). There's no way I can see to automatically exclude it from my stats so I'm trying to keep the hit-count down on it.
Periodically I'm getting into the database and manually deleting any entries against it from my Post_statistics table!
# Scott Cate
27/01/2009 12:42 AM
If you get into any code, you can delete all the statitics by post id, with this ... if 100 is your post id.
PostStatistic.Destroy(PostStatistic.Columns.PostId, 100);
# mabster
27/01/2009 7:59 AM
Thanks Scott! I'll dig up the code and include this call, because my 404 page is almost always my "most-viewed" page!
# Scott Cate
2/02/2009 2:04 AM
I threw together a quick assembly that removes the 404 from your stats, as well as shows a nice report of what the 404 missing links are.
Thanks for your help in getting me started.
scottcate.com/.../graffiti-404-ma
# Italian Translation
8/08/2009 9:53 PM
Graffiti is way better then BlogEngine.I changed my blogs using it.The only problem is that this software is still not so popular and there are not mnay plugin
# English to Italian translation
17/05/2010 1:15 AM
This is a commendable solution to dealing with 404 pages.