Custom 404 Page for Octopress: Quick Tip Articles,Tips & Tutorials

Custom 404 page for Octopress

[su_divider top="no"]

I have a personal blog built with octopress and hosted on Heroku. I was watching Google Analytics and noticed that several visitors were going to pages that no longer exist. When I went to view the default 404 page, it was just “Sorry, I couldn’t find URL”. I wanted to provide a better, custom 404 page for Octopress with links to certain things they might be looking for.

So, I did some research and found out it’s super easy to add a custom 404 page for Octopress or even just plain Sinatra. In your file, you should have:

not_found do
  send_sinatra_file('404.html') {"Sorry, I cannot find #{request.path}"}

So, for Octopress, it’s already configured to handle 404 pages and you just need to create the page. For Sinatra applications, you can use that code as well wherever you define your routes for your app (in somewhere inside your Server class)

To create the page, make a new file named 404.markdown in your /sources directory. I just copied the Yaml header from one of my pages, but here it is anyways:

layout: page
title: "404 Error"
date: 2012-10-26 16:25
comments: false
sharing: false
footer: false

After that, add some text explaining that the page couldn’t be found and instead offer some links. You can take a look at my 404 page right now to get an idea. If you’ve never tried out Sinatra, what are you waiting for. It’s great and way more lightweight than Rails.

Custom 404 Page for Octopress: Quick Tip
0 votes, 0.00 avg. rating (0% score)
Banner Ads Post