Dec
23rd

WordPress 404: Page Not Found? Try these links…

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:
http://www.microsoft.com/ajax
Helpful, huh?

Now try it on Yahoo!:
http://www.yahoo.com/ajax
Ugh!

How about our hero, Google:
http://www.google.com/ajax
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:
http://www.douglaskarr.com/ajax

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!

RSS feed | Trackback URI

17 Comments »

Comment by no imageAde (sezwho)
2006-12-23 11:37:12

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.douglaskarr.com/ajax to see what I mean.

Rate this:
2.5
 
Comment by no imageAde (sezwho)
2006-12-23 11:39:25

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.
Rate this:
2.5
 
Comment by no imageBrandon Wood (sezwho)
2006-12-23 12:14:50

Thanks for posting this Doug. I had thought of doing something similar to this, but just hadn’t gotten around to coding it yet. Now you’ve made my job much easier :)
Rate this:
2.5
 
Comment by no imageDouglas Karr (sezwho)
2006-12-23 12:58:34

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!
Doug

Rate this:
2.5
 
Comment by no imageAde (sezwho)
2006-12-26 09:40:53

That’s a good idea for a blog post. I think I’ll work on something like that.

In the meantime, here’s what Wikipedia has to say:

http://en.wikipedia.org/wiki/SQL_Injection
http://en.wikipedia.org/wiki/Cross_Site_Scripting

Rate this:
2.5
 
Comment by no imageBrandon Wood (sezwho)
2006-12-27 17:52:53

For anyone who is interested, I put together a Beginner’s Guide to Understanding SQL Injection.
Rate this:
2.5
 
Comment by no imageDouglas Karr (sezwho)
2006-12-31 01:47:01

Tonight I noticed that there is a related posts plugin that also has 404 support here:
http://wasabi.pbwiki.com/Related%20Entries
Rate this:
2.5
 
Comment by no imageRick Cockrum (sezwho)
2007-01-24 19:56:50

Thank you, Doug. This is the most useful thing to do with a 404 page I’ve seen.
Rate this:
1.4
 
Comment by no imageDouglas Karr (sezwho)
2007-01-24 20:36:39

Thanks, Rick! I think it’s helpful, too… especially if you’ve moved some posts or deleted them.
Rate this:
2.5
 
2007-01-25 21:56:35

[...] I got the chance to put my idea into action, Doug Karr did it for me. He wrote a very simple script that does exactly what I had envisioned: it takes the keywords from [...]
 
2007-01-30 07:24:17

[...] search results to his 404 page, using the keywords from the broken URL. With a small script from Doug Karr, Brandon was off and [...]
 
2007-06-22 10:20:05

[...] A new 404 page template by Douglas Karr [...]
 
2007-07-08 12:41:26

[...] tested a URl, like http://www.hamelife.com/parenting that an ordinary 404 page comes up. Check out my post on customizing a 404 page - it’s almost like creating a custom search engine for your [...]
 
2007-07-26 21:35:29

[...] to do it? I think a little humor goes a long way, and certainly an apology for the inconvenience. Douglas Karr has a nice solution using the Related Posts plugin that pulls the words in the URL that was entered in error, and brings back related entries on your [...]
 
2007-09-10 11:18:53

[...] 我參考了The Marketing Technology Blog上的說明,修改了自己的 404.php 如果你想變成那樣, 那麼去下載相關的文件 [...]
 
Comment by no imageLearn Spanish (sezwho)
2007-10-03 15:29:13

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 ;)
Rate this:
1.4
Comment by no imageDouglas Karr (sezwho)
2007-10-06 06:46:05

True… but they should at least throw advertising on it!
Rate this:
2.5
 
 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

My Comment Policy: I moderate comments. Please be patient:

  • Spam will happily be destroyed.
  • Use your real name, not some keywords. Otherwise it will be destroyed.
  • Mean comments aren't necessary. If I don't post them I will reply personally to let you know why.
  • Lewd comments will be edited, I don't want my readers leaving because of offensive content.
Great debate, criticism and colorful commentary is always appreciated and approved!