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.
Download
Get Smart 404 over at the Smart 404 Wordpress Plugin page!
Changes
Version 0.3 now released:
- Stops the inbuilt Wordpress 404 handler interfering: Now, will always display the 404 page with suggestions if more than one item matches the search. Previously, Wordpress would redirect if the search matched the beginning of a post title
Installation
- Unzip the package, and upload smart404 to the /wp-content/plugins/ directory on your Wordpress site.
- Activate the plugin through the ‘Plugins’ menu in WordPress.
- Optionally, alter your theme’s 404.php template to list suggestions from Smart 404
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.
Related posts
- Taking the 404 further Update: I have now written a Wordpress plugin that does...
- Private Tags Wordpress Plugin The “Private Tags” Wordpress plugin allows users to specify a...
- Smart redirects A PHP script to be placed in media/upload directories to...
- Hidden Tags Wordpress Plugin Hide certain tags/categories from the public...
- “Elegant Grunge” Wordpress theme An unwashed yet crisp Wordpress theme with a feature footer,...
72 Comments
This started working for me after a day or so. Like, magic! :)
I got the same problem. Not working on 2.7.1, at least I notice no effect whatsoever… what could be going wrong?
This is working for me in 2.7.1 on a Sandbox based theme with the full loop in my 404.php file.
But, my site is a migration from a handcoded aspx site where all blog posts had a url like: blogs_Blog Title.aspx and Smart404 is not detecting the blog title to give a good match unless I strip out the blogs_ part of the URL.
Is there a way I can change the smart 404 behavior to ignore the "blogs_" – I've tried adding blogs_ and .aspx to the ignored terms but that doesn't help as I believe it's controlling the search index, not the incoming search terms to detect.
Thanks, David
Hi David,
Adding blogs_ and .aspx to the ignored terms is exactly right – I'm not at all sure why it wouldn't be working for you. Did you enter each on a new line?
Thanks.
I just tried it again and it's not working, you can see for yourself: http://soiveheard.com/blogs_Felsenstein.aspx results in the standard 404, but http://soiveheard.com/Felsenstein gives me the Super404 resutls.
I've tried turning off caching thinking that could be the issue, but it doesn't see to make a difference.
I take back the part about seeing for yourself, I've since gotten around it with a little mod_rewrite, but I'd still like to figure this out if you can.
I can turn off the mod_rewrite if you'd like to see it not working too.
For those of you working with other plugins that use the tags on titles like <!– :en –> for other languages or hat ever, you need to change this line for it to display right – add the "_e()" function:
Line # 229 in smart404.php:
<a href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a>
to:
Thanks for the tip, Moonsand – I've fixed up the HTML and removed the other comments. For future reference, all of the <, > chars, etc, are escaped automatically (as other commenters were assuming this was already happening, and it was mucking things up)
Moonsand, can you make a picture with this exact code? I dont understand this :) Waiting for link :)
Nemezis, you can open up the 'smart404.php' file in a text editor of your choice (you may want to FTP it to your local computer for editing first, then FTP it back in place afterwards); find the line Moonsand indicates (just search for it if you don't have line number markers), then copy the replacement line and paste it over the original.
Basically wrap the "$post->post_title" inside "_e()" like this: "_e($post->post_title)" this will make wordpress "evaluate" the text and output the correct text according to any other plugins that need to modify the text like qtranslate.
Got it! I think this could be helpful ;) Thanks very much!
Here is what i use in my 404. From GOOGLE:
<code> <script type="text/javascript"> var GOOG_FIXURL_LANG = 'en'; var GOOG_FIXURL_SITE = 'http://sports2all.com/'; </script> <script type="text/javascript" src="http://linkhelp.clients.google.com/tbproxy/lh/wm/fixurl.js"></script></code>
That's some great functionality from Google – thanks for the heads-up! Would be great to use with static sites, etc, in particular.
For anyone else who's curious, here's a <a href="http://googlewebmastercentral.blogspot.com/2008/08/make-your-404-pages-more-useful.html">post at Google Webmaster Central</a> explaining it
SuperMAG, can you tell us more about this code please? What it does?
Hi, Thanks for your plugin.
I am using WP 2.7.1 and your plugins works fine.
After installation, I've changed my permalinks (removing yyyy/mm/dd) and when I go to my old link (the one with yyyy/mm/dd), it's just redirect it to the new link.
I also customize my 404 error page using your smart404_loop and it works beautifully. Sample error page: http://www.tamanroyal.com/living-room.html
Thanks once again.
Hello, Michael, thank you very much for this plugin. It really works. it was what I needed. The next thing you should do is combining it with an 404 alarms system just like from Alex King: http://alexking.org/projects/wordpress/readme?project=404-notifier Juergen
A small bug I think this works: -> <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; ?> <- this won't, gives you no result <?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; ?>
Hello, Michael. Thank you for such a valuable smart 404 plugin.
I have a problem in excluding thank you pages by "Ignored patterns". I want to exclude url at http://www.az-net.com/about/thank-you-order/, and I have tried to set the Ignored patterns field like "-order/$", "order/$" or "thank-you" per line but the url is still shown (i.e at http://www.az-net.com/thank-you ). Did I make a mistake? Any help would be really appreciated.
Thanks,
Hi Hideki,
It looks like you’re trying to omit a page from the 404 results – this isn’t actually what ‘ignored patterns’ is for. Sorry, that’s probably bad documentation on my behalf. The ignored patterns setting is there to ignore certain text from the URLs. For example, if your old permalinks looked like “/203-some-post-title.html”, then you want to ignore the number at the start, and the .html at the end (actually the .html is a built-in ignored pattern, so you don’t need that) – so you’d have a pattern like “^[0-9]+-?” to ignore one or more numbers at the start followed by a hyphen.
There’s currently no way to omit items from the 404 results, although you can do that fairly easily in the loop, something like:
This would exclude posts/pages with the ids 100, 101, 200 or 230.
Michael, thank you very much for your reply.
I tried to add the codes but I still can't fix my problem. I should know how PHP works and then I may try with my poor PHP knowledge. I would really appreciate if I may hear which I must change the codes, the 404 template or the plugin itself.
Thanks,
You'll want to edit 404.php in your theme – see the readme for Smart 404 for more info on the process. Best of luck!
Michael, thank you very much for your kind.I will try it. Thanks,
Hello, I hope the server environment may be a hint.
FreeBSD 7.1-RELEASE-p5 i386 Apache/1.3.41 PHP 5.2.8 MySQL 4.0
Thanks,
Plugin works fine. But I noticed that when the old permalink with /trackback is accessed, it gives 404 error. Google and other search engine have indexed my posts with /trackback, but when visitors clicked that it gives 404 error. But when accessed without /trackback it redirects with the new permalink. how do i fix this? Please help me. thanks!
Good catch! You're right, this is something I'd missed. Version 0.3.2 just released fixes this.
0.3.2 works fine with the /feed/ it d0esnt give any 404 error, but still noticed that there is still a bug on /trackback/ it still gives an 404 error. Please check on this. Thanks!
Hi,
I'd just changed my permalink and changed .htm to / and the plugin works fine, it is now redirecting my old permalink to the new ones.
I'm experimenting with your plugin now. I wish it could include pages but it's still useful otherwise. I found a function to display the URL on page but this might be something you could add to the plugin itself. Thanks for this!
Oh! One question – is there a way to display a message if there are no suggestions?
Hi Cole – certainly. Something like this should do it:
<?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; else : ?> **There were no suggestions** <?php endif; ?>A combination of being tired, dense and generally unfamiliar with PHP has me stumped. Is there something more specific to make that work? With the added else, it still just displays most recent posts in the loop when no relevant entries are found.
I work with WP 2.7.1. I’ve opened my 404.php and below my normal text concerning the error message 404, I’ve put . But nothing happens. The error page for itself works, but no suggestions are shown.
Again, the code has not been shown in my post above (before and after are the breaks, of course): ?php smart404_suggestions() ?
No suggestions, I have put in your code of configuration the theme as mentioned above, but … nothing to see: http://kunstblog.birgit-heinz.de/2009/04/ueber-die-kunst-kunst-zu-verkaufen2/
Okay, something is not right.
Using WP 2.8
Your plugin seems to have issues with .html at the end of URLs
If I go to http://www.sanglier.co.uk/baking, it finds my couple of articles with baking in them.
If I try http://www.sanglier.co.uk/baking.html, then it cannot find any results and just puts up the most recent posts.
I notice that the script is meant to be removing the .html, but I assume this is not happening?
Help?
Hi Joss,
What do you see if you put the following debugging statement into smart404.php on line 55, just before the line
if ( in_array( 'posts', (array)get_option( 'also_search' ) ) ) {:echo "search: $search";Ah! I see the problem. In a few minutes, version 0.3.3 will appear – update and it should solve that.
Hi Michael
It tells me it is searching for “baking.html”
2 Trackbacks
[...] Плагин позволяет вместо того чтобы показать пользователю обычную страницу об ошибке – попататься угадать что же он хотел увидеть на этой странице. Пользователь увидит результаты поиска ваших постов, тэгов и категорий используя ключевые слова из URL с которого он попал на эту страницу ошибки. Если есть совпадение, то пользователь сразу пересылается на целевую страницу без показа ошибки 404. Если совпадений больше чем одного, то 404 шаблон может использовать несколько тэгов для показа различных ссылок пользователю. Если вам это интересно, то пожалуйте на страницу автора плагина. [...]
[...] Smart 404 for Wordpress – плагин воспользуется поиском и выдаст пользователю список страниц, подходящих под набранный им урл, вместо 404-ой ошибки. [...]