1

All subcategories in WordPress create 2 urls to same page.

domain.com/parentcat/childcat/

domain.com/childcat/

both urls go to the same page. in the eyes of Google this is duplicate content.

it seems it would be logical for WordPress to redirect domain.com/childcat/ to domain.com/parentcat/childcat/

Using WordPress 4.3 no plugins.

I can't seem to find a fix for this anywhere. Any help would be appreciated.

1 Answer 1

1

This can get a bit demanding to get it right so this following snippet should provide the basic upon which you can research and build a more complete solution:

add_action( 'template_redirect', function() {
    if ( is_category() ) {      
        $term = get_queried_object();

        if ( isset( $term->parent ) && $term->parent ) {
            global $wp;

            $request_url = trailingslashit( get_site_url( get_current_blog_id(), $wp->request ) );
            $link = get_term_link( $term );

            if ( ! is_wp_error( $link ) ) {
                if ( strpos( $request_url, trailingslashit( $link ) ) !== 0 ) {
                    wp_safe_redirect( $link, 301 ); 
                }                   
            }
        }
    }
} );

This solution will work with pagination too but it will redirect to the start of pagination in case you are attempting to access an "incorrect" sub-category paged URL (http://www.example.com/category/child/page/2/ will redirect to http://www.example.com/category/parent/child/ -- this can be addressed by checking if we have a paginated request and build the redirect link accordingly)

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.