0

I recently deleted a large number of tags (~20k-25k) from one of the categories that publishes news items. These tags were contributed by our authors and added no value to the site. As a result, Google's bot is now noticing a LARGE number of 404 errors (~90k) on our site and as a results has pushed our rankings down. Though Google says that 404 errors don't affect, our observation is bit different.

After experiencing the downwards traffic for over a month now, I've decided to redirect all the 404 pages originating from the deleted tags to home-page permanently. I'm not sure whether this is a right step; but that seems to be the only option that I have.

My requirements:

For every 404 resulted on domain.com/tag/ , I want to setup 301 redirect to home page.

I'm a complete noob when it comes to redirects and can't take chances on messing up with these.

1 Answer 1

1

Sounds like an easy task to do. I think hooking into 'template_redirect' and conditionally triggering redirect is the way to go.

EDIT: Seems is_tag() isn't reliable once 404 is triggered. That's why we need to look into the query vars to figure out if it's really a tag query or not.

function redirect_404_tags_wpse67077() {
    global $wp;
    if ( ! empty( $wp->query_vars['tag'] ) && is_404() ) {
        wp_safe_redirect( home_url('/'), 301 );
        exit;
    }
}

add_action( 'template_redirect', 'redirect_404_tags_wpse67077', 1 );
5
  • Where should I add that code? In the functions.php under currently active wordpress theme?
    – TheBigK
    Oct 4, 2012 at 10:03
  • Yep, that should do it
    – Z. Zlatev
    Oct 4, 2012 at 12:53
  • Doesn't seem to work. I added it to the functions.php under the active theme folder and even tried it with the 'custom_functions.php' that accepts custom functions. Something's wrong.
    – TheBigK
    Oct 4, 2012 at 13:35
  • I've updated my answer
    – Z. Zlatev
    Oct 5, 2012 at 5:30
  • Did this end up working for you with the update @TheBigK?
    – Tim Malone
    Jun 5, 2016 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.