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:
Download
Get Smart 404 over at the Smart 404 WordPress Plugin page!
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
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.
35 Comments
I’ve been able to confirm that this plugin, combined with the 404 notifier you mention, does indeed mail, even though the 404 is found. I’m running the latest version (as of this moment) of both WordPress and both plugins.
I liked this plug-in, almost fixed all my URL problems caused by the permalink change that I had to make this mounth, but my site also have a lot of attached files and there lies a problem, It would be nice if it had the option to search in attachment posts too. I readied that you are without time to work in the plug-in, but if you could give some pointers in how to work this out, I would be very glad.
Anyway, nice work with the plugin, tks for the attention.
Just installed the plugin and created a custom 404 page.
The suggestion list is ordered with the latest post first. I want it ordered with the ‘most relevant’ first. Is that possible?
Anyways, nice plugin.
Thanks, Biju
The plugin is a very good one. I have not come across a better way of tackling 404 pages – it satisfies the requirements of SEO (from the cursory study I made about how the plugin works) and of course it is an excellent way to ensure the website visitor does not land at a dead end.
Keep up the good work. I was curious to know how long have you been working with WordPress and please suggest a resource to use to become a WordPress expert.
Thanks in advance.
Awesome Plugin buddy….. It helped me a lot when i shifted my blog from blogger to wordpress..
A special thnx to you guyz… :)
my site had a 404 error and i was looking for a plugin and found this one. i wanted to see some screenshots or some demo about how this plugin works?
any such examples please? i came to this plugin page from shoutmeloud recommendation
venkata, i’m using this on my website, and have made some enhancements for fine-tuning & debugging…have a look @ http://cavitronix.com/toms/smart-404
Hello Tom,
thanks for your rely..
when i click on the link you provided it goes to this url, http://wibles.webhop.net/toms/stuff/smart-404
is this how the 404 plugin works?
sorry i thought i’d replied sooner…
the link to my personal website contains my modifed smart404 files; to see it in action, go to http://cavitronix.com/this/doesnt/exist
Hello Tom,
yeah, that is what exactly i was looking for…
thanks a lot :)
This plugin is pretty good, but seems a bit stale. It also seems to fail to find a relevant substitute, or sometimes ends up with only one odd suggestion and redirects to it. Some stemming would be handy e.g; a renamed /teaching does not seem to end up finding /teach as a suggestion.
/teaching does not seem to end up finding /teach as a suggestion.
perhaps the Ignored patterns regexp could take & execute a substitution of null for common suffixes? ie: /*ing//
let me know if it doesn’t, & i might take a look;-)
hmmm, maybe the Ignored patterns regex can b futzed 2 remove common suffixes: /ing//
This is indeed possible – you just need to configure the plugin to do so, I think.
Add the following line to the “Ignored patterns” field in the plugin’s settings, and that should do it:
(ing|s|er|est)$
just tried that & it doesn’t seem 2 work…i’ll look into how ig.pats work when i get a chance
just noticed this in my error log:
PHP Fatal error: Call to undefined function add_action() in smart404.php on line 392
which is:
add_action( ‘template_redirect’, ‘smart404_redirect’ );
nevermind, it was in a non-wp directory, tried 2 load it from the url
oops, 1st post resulted in a 500 error, so i reposted…looks like both took;-)
Hi,
what is the easiest way to turn off the redirection and just keeping the suggestion?
Cheers!
Comment the lines:
at the bottom (add “//” at the beginning of the line).
Hey, thank you, but your solution does’nt work.
Sorry, it does work but i dont get any suggestions.
I already changed query_posts( array( “post__in” => $postids ) );
to
query_posts( array( “post__in” => $postids, “showposts” => 10 ) );
but it still show 5 related posts instead of 10. How to fix this?
I think Smart 404 is redirecting from pages that it shouldn’t be redirecting from. How can I look into this?
ignored patterns seems to be ignored;-}
[email protected]@i,@/(trackback|feed|(comment-)?page-?[0-9])/[email protected],@\\.(html|php)[email protected],@/?\\?.*@i [uri=/about-us/wp-xxx= search_words=wp xxx
perhaps i’m misunderstaning regex/array_map/create_function?
output from my debug:
smart404_redirect: ignored_patterns_input=wp-.* (ing|s|er|est)$ smart404_redirect: [email protected]* (ing|s|er|est)[email protected],@/(trackback|feed|(comment-)?page-?[0-9]*)/[email protected],@\.(html|php)[email protected],@/?\?.*@i
so why isn’t
$patterns_array = explode( '\n', $patterns );
creating elements like ` $patterns_array[] = “/?\?.*”;’ does so that the array_map creates separate patterns?fixed it:
$patterns_array = preg_split('/\n|\r/', $patterns, -1, PREG_SPLIT_NO_EMPTY);
instead of
$patterns_array = explode( '\n', $patterns );
Is there anyway redirect the search term to http://www.webiste.com/?s=searchterm instead of showing the result at 404 page.
You can do that in a 404 template, with something like:
$term=str_replace("_", "-", basename(trim(urldecode($_SERVER["REQUEST_URI"])))); query_posts(array("s"=>$term));
well, it works sometimes: with only “wp-.*” in ignored patterns http://cavitronix.com/about/wp-xxx.php correctly finds http://cavitronix.com/about-us but adding “index.php” to ignored patterns breaks it: http://cavitronix.com/about/wp-index.php returns: wp index in the search terms…i’ll dive into regex when i get a chance…
I have added a patch to the wordpress.org forums that: 1) removes unnecessary calls to exit 2) provides a saner redirection behavior for content matches [iff there is one match in posts AND pages] 3) try to ignore sticky posts 4) note that code could simplified if options were tweaked to store singular form [post V posts]
Mike,
Noticed that the Infinite Scroll plugin causes smart 404 to show a random post when reaching the end of an archive.
from the infinite scroll site “Infinite scroll is configured to die when it hits a 404 Not Found status code, so when it goes through all your archives it should hit a /page/43/ (or something) that doesn’t exist, then show a message “Congrats, you’ve reached the end of the internet.” “
Can you think of any way round this?
sorry, i don’t use infinite scroll…is there some way i.s. requests can be identified? by referer perhaps?
Hi,
I have using a custom 404.php page that will redirect all 404 page request to home page. here is the code of 404.php file.
You can use the same..
Thanks…
Thanks for sharing Info…..! The latest version doesn’t work at all for me either. I tried disabling all other plug-ins, and that didn’t help. The 404 script doesn’t show anything. Same with my other site at http://www.freecontactlensesguides.com. It worked just fine before!
Could it be a conflict with the Thesis theme perhaps?