Redirecting WordPress 404 errors

Interestingly, there turns out to be a built-in mechanism for attempting a redirect on 404 errors in WordPress. It is amazing but this function is built in and it is called redirect_guess_404_permalink(). It is even more amazing that theme developers do not include it as standard in their 404 landing pages.

The function works magic trying to process the given URL and figure out what post is actually required. You may have changed the structure of your permalinks but this function will figure out the new URL of the post if it is not too complicated. At least, you should include it every time just on an off-chance that it helps.

Add to the “functions.php” of your theme this snippet:

 * Try to find the right page when it comes to 404
function my_redirect_404() {
   if( is_404() ) {
     if ( $new_url = redirect_guess_404_permalink() ) {
       wp_redirect( $new_url );
add_action( 'template_redirect', 'my_redirect_404' );

Most of redirects will then be automagically taken care of.… -->

continue reading →