Blog

Smart 404 for WordPress

Save visitors to your WordPress site from unhelpful 404 errors!

When a page cannot be found, Smart 404 will use the current URL to attempt to find a matching page, and redirect to it automatically. Smart 404 also supplies template tags which provide a list of suggestions, for use on a 404.php template page if a matching post can’t be immediately discovered.

Instead of quickly giving up when a visitor reaches a page that doesn’t exist, make an effort to guess what they were after in the first place. This plugin will perform a search of your posts, tags and categories, using keywords from the requested URL. If there’s a match, redirect to that page instead of showing the error. If there’s more than one match, the 404 template can use some template tags to provide a list of suggestions to the visitor.

This plugin is also useful if you have recently changed your permalink structure: With minimal or no adjustment, old permalinks will still work.


My latest project:

The Cartographer banner


Download

Get Smart 404 over at the Smart 404 WordPress Plugin page!

Installation

  1. Unzip the package, and upload smart404 to the /wp-content/plugins/ directory on your WordPress site.
  2. Activate the plugin through the ‘Plugins’ menu in WordPress.
  3. Optionally, alter your theme’s 404.php template to list suggestions from Smart 404

Note: If you desire reporting on 404 errors that Smart 404 is unable to remedy, I recommend Joe Hoyle’s JH 404 Logger, which adds an item to your dashboard listing 404 errors. 404 Notifier by Alex King will send emails for 404 errors, but I hear reports that emails are sent for 404 errors that this plugin is able to recover from, not just unrecoverable errors.

Configuration

There are two configuration options for Smart 404:

Search

Turn on or off searching of posts, pages, tags and categories

Ignored patterns

A newline-separated list of terms or patterns to ignore from the URL. This is particularly useful for supporting old permalinks with an ID number in them. For example, to work with URLs like:

123-post-title.html

Add the regular expression pattern:

^[0-9]+-

This will ignore all numbers, followed by a hyphen, at the start of the URL.

Template Configuration

To provide a helpful list of suggested posts in your 404 pages, modify the 404.php template in your theme to use a Smart 404 template tag. For example:

<?php if (smart404_has_suggestions()) : ?>
Try one of these links:
<?php smart404_suggestions(); ?>
<?php endif; ?>

Or, for something a little more complicated:

<?php if (smart404_loop()) : ?>
<p>Or, try one of these posts:</p>
<?php while (have_posts()) : the_post(); ?>
<h4><a href="<?php the_permalink() ?>"
  rel="bookmark"
title="<?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h4>
  <small><?php the_excerpt(); ?></small>
<?php endwhile; ?>
<?php endif; ?>

Note that smart404_loop() will only work for posts, not pages, due to limitations in the loop mechanism. Several template tags are supplied by Smart 404 for use in the 404.php template:

smart404_has_suggestions

Returns true if there are some suggestions, false otherwise

smart404_get_suggestions

Retrieve an array of post objects for rendering manually.

smart404_suggestions

Draw a list of suggested posts.

Pass the parameter “list” to render suggestions as a list.

smart404_loop

Query posts for use in a Loop. See the second example above for usage. Note that smart404_loop() will only work for posts, not pages, due to limitations in the loop mechanism.

, , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

31 Comments

  1. Posted September 27, 2008 at 5:17 am | Permalink

    This plugin works Great! Thanks so much. I’m using it on my site now http://www.dealfark.com

  2. Posted September 27, 2008 at 11:40 am | Permalink

    This is splendid! Thanks!

  3. Posted September 27, 2008 at 3:47 pm | Permalink

    Very clever plug-in — thank you very much for making it available ; )

  4. Deuce
    Posted September 28, 2008 at 1:31 am | Permalink

    I wish this would work for Pages too. I did everything you said to do and I don’t have any posts, i use WP as a CMS so i only have pages and it doesn’t work. Hope you can add pages!

  5. Posted September 28, 2008 at 12:19 pm | Permalink

    Deuce: Your wish is my command. Version 0.2 now supports pages.

  6. Posted September 28, 2008 at 7:05 pm | Permalink

    Thanks! I use it for my site! Great idea!

  7. Posted September 29, 2008 at 4:26 pm | Permalink

    Is this 2.6 compatible? I FTP’d it to my plugins directory, but when I go to WordPress Plugins it doesn’t even appear as a plugin that I can activate.

  8. Posted September 29, 2008 at 4:35 pm | Permalink

    It certainly is, Chris, although it’s only been tested on 2.6.2. Did you upload the smart404.php file into wp-plugins?

  9. Posted September 30, 2008 at 12:51 pm | Permalink

    very Nice ! tnx

  10. Posted October 2, 2008 at 7:30 am | Permalink

    Installed on my new website to test it. Works great.

  11. Posted October 6, 2008 at 11:45 pm | Permalink

    You should consider adding this plugin to the WordPress Extend Plugins Directory (http://wordpress.org/extend/plugins/) so we can be notified of your updates, and plus you’ll get more exposure. =)

  12. Posted October 7, 2008 at 7:53 am | Permalink

    Believe me, drew, I’ve tried http://wordpress.org/extend/plugins/smart-404/ =) Almost two weeks now and not a peep from the WP folks. Guess they’re busy

  13. Posted October 11, 2008 at 7:36 pm | Permalink

    Impressive. Very nice work Michael, works like a charm on my blog

  14. puneet
    Posted October 13, 2008 at 12:56 am | Permalink

    hey mike check out http://www.midnightedition.com we just shifted our site to new server and new platform due to which we r almost 8000 404 errors daily. we r using ur plugin. but it still isnt giving right answers for everything. example http://www.midnightedition.com/poem there r over 15000 poems here but yet it cant pick anyone up. same with lots of other 404 errors any clue why also when i http://www.midnightedition.com/health its opening pages which have nothing to do with health.

    what do u suggest i do?

  15. Posted October 13, 2008 at 10:35 am | Permalink

    Hi Puneet,

    I had a big reply explaining how to get around what I saw as a WordPress restriction, but I just discovered a way around it =)

    Your first example has an easy fix – just disable searching of tags and categories in Smart 404 settings, as it appears you want lists of posts, not tags, which currently what’s coming up for ‘poems’.

    For your second example: This is behaviour that’s built into WordPress – it will automatically try to redirect to a post that starts with the requested text, without even touching Smart 404. However, there’s a fairly elegant way around it: I’ve made available a beta version of Smart 404 containing some stuff to block this behaviour. Grab it here. Please perform a number of tests to make sure it’s behaving as expected – including testing existing working links, as well as some of your 404s. I will wait to hear from you before I release this as the next version, so please do let me know how it goes.

  16. Posted October 28, 2008 at 8:11 am | Permalink

    “thanks”

  17. Posted November 14, 2008 at 3:50 am | Permalink

    basic functionality works in wp-2.3… so thanks!

  18. Posted November 18, 2008 at 12:50 pm | Permalink

    Great idea! Unfortunately it doesn’t appear to work on IIS. I even tried commenting out the is_404() check but then it went into an infinite loop (makes sense :-)). For some reason on IIS is_404() never returns true – not sure why.

  19. Posted December 11, 2008 at 1:51 pm | Permalink

    Just a note, this plugin appears to work fine in 2.7 – I just upgraded my blog and it seems to be working fine. Let me know if there are problems.

  20. Posted December 17, 2008 at 4:53 pm | Permalink

    G’day Mike

    Are you getting any reports of this NOT working on GoDaddy Hosting. I am using both 2.65 and 2.7 on another site – both on GoDaddy and dying?

    Dave

  21. Posted January 13, 2009 at 5:32 am | Permalink

    One page of mine was getting so many clicks from Digg everyday, but because of my stupidity I changed the permalink structure and the link from Digg was showing a 404 page.

    But ur plugin had become my savior and its redirecting to the exact post which was supposed to be seen. Lots of thanks to you Mike.

  22. Posted January 14, 2009 at 3:06 am | Permalink

    Hi, firs of all, great plugin, I love it so far, but I cant make the regular expressions work. I want to remove the ID from the old URL but the example you put doesn’t work for me :(

    For example, I want this URL: http://blog.pnikosis.com/archives/133-some-text.html

    look for: some text

    I tried with: ^[0-9]+- and *[0-9]+-

    But no good results, in the title page I can see that it hasn’t removed the numbers, and in the second case, it gives me an preg_replace() error. Does anyone can help me?

  23. Posted February 10, 2009 at 9:26 am | Permalink

    GREAT plugin! Is there a way to show 10 related posts instead of 5? :P

    Take a look how it is working at my blog: http://wptests.hikarinet.info/404/bunny/ http://hikari.ws/404/mesario/voluntario/

    • Posted February 10, 2009 at 9:51 am | Permalink

      Thanks Hikari! Yep, there should be – try changing line 262 of smart404.php:

      query_posts( array( "post__in" => $postids ) );

      to

      query_posts( array( "post__in" => $postids, "showposts" => 10 ) );

  24. Posted February 10, 2009 at 8:27 pm | Permalink

    Doesnt working on WP 2.7 Can we wait for updates?

  25. Posted February 10, 2009 at 9:08 pm | Permalink

    I’ve added required code to my 404.php page. But when I;m entering wrong link it return zero results. Try this: http://new.electrokilla.com/topic/6010/deadmau5-new-tunes-promo-preview.html PS: I want to move new site located at new.electrokilla.com to main domain electrokilla.com. And there is a lot of old links in search engine DB. So even page cannot be found the visitor can find what is he need on a new site. So it doesn’t work for me fine.. Any help?

    • Posted February 12, 2009 at 10:20 am | Permalink

      I’m sorry for the delay in getting back to you! For starters, what web server (i.e. apache/IIS?) are you using, and what version of PHP and WordPress? What code are you using on your 404 page to list suggestions? If you enter a URL which corresponds to a post, does it redirect properly? For example, this page can be reached by going to http://atastypixel.com/blog/smart-404-for-wordpress.

  26. Posted February 12, 2009 at 8:19 pm | Permalink

    PHP : 5.2.1 Server: Apache/1.3.37 (Unix) Wordpress: 2.7.1 EN

    I’m using this code on 404.php:

    Try one of these links:

    Look, I have an old site which has links in another format. This links from the old site and links from the new WordPress site are different. And I want to make visitors happy if they comes from google and get 404 page, cuz on this page they can find what they need easily. This page should give some suggestions to visitors based on doesn’t exist anymore 404 page.

    I’ll give you example: I’m looking for a “deadmau5 new tunes” in google. I’ve got some results, one of them is going to my site http://electrokilla.com/topic/6010/deadmau5-new-tunes-promo-preview.html. Everything looks great, an old site still works. But soon I will delete an old site and replace them by a new WordPress blog which includes all data (posts, articles) from the old site. Your plugin gives me suggestion: “Don’t worry, you can find it here: http://electrokilla.com/2009/02/10/deadmau5-new-tunes.html

    The problem is giving suggestions to a visitor. If old link doesn’t work this plugin gives suggestions to visitors with new wordpress links. This is my explanation :)

    PS: the plugin still doesn’t work)) gives 0 suggestions.

    • Posted February 12, 2009 at 10:02 pm | Permalink

      Very strange, it appears to be doing nothing at all – even the redirection doesn’t seem to be working. In Smart 404 configuration, do you have at least ‘Posts’ ticked? If it’s not that, I’ll make you a debug version to test with soon, figure out what’s going on.

  27. Posted February 12, 2009 at 8:21 pm | Permalink

    Looks that my php code was not parsed correctly ‘?php if (smart404_has_suggestions()) : ? Try one of these links: ?php smart404_suggestions(); ? ?php endif; ?’

  28. Posted February 13, 2009 at 1:47 am | Permalink

    ‘Posts’ are on. Will wait for your version.

2 Trackbacks

  1. […] Плагин позволяет вместо того чтобы показать пользователю обычную страницу об ошибке – попататься угадать что же он хотел увидеть на этой странице. Пользователь увидит результаты поиска ваших постов, тэгов и категорий используя ключевые слова из URL с которого он попал на эту страницу ошибки. Если есть совпадение, то пользователь сразу пересылается на целевую страницу без показа ошибки 404.  Если совпадений больше чем одного, то 404 шаблон может использовать несколько тэгов для показа различных  ссылок пользователю. Если вам это интересно, то пожалуйте на страницу автора плагина. […]

  2. […] Smart 404 for WordPress – плагин воспользуется поиском и выдаст пользователю список страниц, подходящих под набранный им урл, вместо 404-ой ошибки. […]