Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a WordPress site which syndicates content from a number of sources and auto creates categories and tags.

I have converted a large number of categories to tags which now leaves a ton of categories showing 404's. I started manually redirecting these but as expected this is a huge task.

I'm trying to find a way to use htaccess redirects, which can

  1. detect all category 404s
  2. redirect those to their /tag/tagterm/ equavalent

so all 404'ing /category/catterm to be redirected to /tag/tagterm equavalent

Any ideas?

share|improve this question

1 Answer 1

Heree's a code solution, not htaccess. This first checks if current query is for a category page, then checks category existence, and if category not exists but a tag with the same name, it redirects to the tag page.

add_action('parse_request', 'wpse_parse_request');
function wpse_parse_request( $r ){
    if( isset($r->query_vars['category_name']) )
    {
        $cat = get_term_by('slug', $r->query_vars['category_name'], 'category');
        if( !isset($cat->term_id) )
        {
            $tag = get_term_by('slug', $r->query_vars['category_name'], 'post_tag');
            $link = get_term_link($tag);
            if( !is_wp_error($link) )
            {
                wp_redirect($link);
                exit;
            }
        }
    }
}
share|improve this answer
    
Thanks so much for this shazzad, does this go in my functions.php file? Will it remove the pages being flagged as 404 from the google bot's perspective? –  user2410690 Mar 6 at 15:57
    
Yes, these could go to functions.php file. And it should handle the googlebot's 404 for future crawl, as we are redirecting with 301 (wp_redirect function use 301 by default) status header, what means moved permanently. –  Shazzad Mar 6 at 16:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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