WordPress 404: Page Not Found? Try these links…

Reading Time: < 1 minute

I really appreciate sites that intelligently try to redirect you to the pages you may be looking for. Let’s say I’m looking for ajax information on a given site, I usually simply try to use the word in the URL to see what happens.

On Microsoft, see what happens:
Helpful, huh?

Now try it on Yahoo!:

How about our hero, Google:
C’mon! You’re a freakin’ search engine for heaven’s sake!

How difficult is it to simply take the words in the URL and come up with some search results that might guide you to the right page? It’s not so difficult!!! In fact, I programmed it into my site tonight in about an hour:

404 Related Posts

I simply used the same logic that the related posts widget code utilizes and applied it on the 404 not found page. It scores the content based on the keywords (there can be more than one) pulled from your URL. Your theme will need to support a 404 page in order to implement it. Also, this one is built for my theme, you may have to make some modifications.

Download the 404.php page here. Feel free to use it where ever you’d like! It does require that you have the Related Posts plugin!


  1. 1
  2. 2

    Great idea! I added it to my site. A couple notes though:

    1) If you don’t have the related posts plugin, you probably haven’t created the necessary fulltext index required by the SQL code here. Run the following against your WP database at a MySQL prompt:

    ALTER TABLE `wp_posts` ADD FULLTEXT `post_related` (`post_name` ,`post_content`);

    2) Doug, you might want to revise the code as it’s vulnerable to SQL injection and cross-site scripting attacks. It’s as simple as using mysql_escape_string() against $keyword in the SQL statement and htmlspecialchars() against $keyword when displaying it on the page. You can see the changes I made at http://blog.olonoh.com/downloads/404.zip (modified for my theme).

    Try http://www.dknewmedia.com/ajax to see what I mean.

  3. 3

    Whoops, that sample URL didn’t come through. Basically add any opening HTML tag at the end of the URL to test. e.g. a BLINK tag.

  4. 4

    Thanks Ade! I updated my page AND the download page. Could you do my readers a favor and write a post on SQL injection /cross-site scritping attacks? I’d like to learn more about how it would have been done in this case and how exactly it works. I’m a little bit ignorant of the topic.

    That’s why I’m a hack and you’re a programmer!

    Thanks, Again!

  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10

    Thanks, I downloaded your code, as I’ve got a couple places where I can use this. And, and for the record. Google is not a search engine, they’re actually an advertising company. So, you can’t really expect them to develop an intelligent 404 page 😉

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.