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.

When you keep the category base ( that is you don't remove it by a plug in) WordPress does allow you to browse to your 2 level deep category in two different ways as the example below would demonstrate it.

say, your category has a top level item "MULTIMEDIA" and a child under it called VIDEO.

Visually, we are talking about a situation like this;

Category taxonomy

multimedia
    video
    audio
topics
    science
    politics 

Well, you may get to that child (video) in the following 2 different ways;

yoursite.com/category/multimedia/video or yoursite.com/category/video

The point here is that WP brings you to the exact same page, regardless of which URL has been used, and there are no redirects here neither, so both URL's remain on the visitor's browser as is just like it was requested at the first place. And with either URL, the resulting content is identical.

That's a hateful situation by Google.

Google punishes such sites cause your web site got the same exact content appearing under 2 different URL's. ( This is of course when you happen to use both URLs. ) Sure, Nobody would want to do that deliberarely but sometimes you end up with that. And this question is all about that case.

Let's say, you wanted to pick the simpler URL for your VIDEO category and you want to stick to that only. Basically, you want the URL to be

yoursite.com/category/video

This is easy to achieve when you use the custom menu feature that comes with WordPress where you can specify a URL for that particular custom menu item of yours. So, when visitors click on the VIDEO link on the custom menu, they end up coming to

yoursite.com/category/video

No problems so far...

But as soon as they pick a post out of that category, and they end up coming to a post where the post's meta data displays things like "CATEGORIZED in" or "TAGGED by" etc, they will be exposed to the alternative URL for that VIDEO category.

So, if a user clicks on the VIDEO link in the "Categorized in" area, the URL that they will end up with would be

yoursite.com/category/multimedia/video

And with that, there goes your SEO ranking.

My question is what techniques do you use here so WordPress always and always use one single URL, the URL you defined in the custom menu.

I thought, category slugs could do this, but when I checked it, slug feature allows me to change only the category term in the URL, not the path to it.

If it were to give me a choice like

video slug: /category/video

I would be done.

But that choice is not available at this moment. When you do that, you end up with the following slug instead

categoryvideo

That's because / is not a valid character for a slug.

Please suggest an alternative way to tell WordPress to stick to /category/video at all times.

Before suggesting the obvious, ( well, just skip the 'multimedia' and make the 'video' a top level cat. ), let me say that that suggestion won't work for our situation.

share|improve this question

2 Answers 2

You could filter 'term_link'.

just an untested idea, written directly into the answer field

add_filter( 'term_link', 'wpse_55476_categore_ur_filter', 10, 3 );
function wpse_55476_categore_ur_filter( $termlink, $term, $taxonomy )
{
    if ( 'category' !== $taxonomy )
    {
        return $termlink;
    }

    // separate the URL
    $parts     = explode( '/', $termlink );

    // get the term
    $term_slug = array_pop( $parts );

    $out       = array ();
    $cat_base  = get_option( 'category_base', 'category' );

    foreach ( $parts as $part )
    {
        $out[] = $part;

        if ( $part === $cat_base )
        {
            $out[] = $term_slug;
            return implode( '/', $out ) . '/;
        }
    }
}

Something like this should work each time a theme or a plugin calls a WP function that generates a category link, because all these functions delegate their work to get_term_link() where this filter is called.

So … part one is solved, part two of your question is: What happens if someone calls the long URL? You could hook into 'wp' and perform the same check (old example). Could be too slow, so the short term link should be stored in a term meta field. Oh, wait … there is no table for that, because … uhm … decisions. :)
But we have some solutions for this too. No need to repeat those here.

Alternative: .htaccess and RedirectMatch

If you have just a controlled and limited set of subcategories (and do not want to change the category part) use Apache’s mod_alias and a simple redirect in your .htaccess or .httpd.conf:

RedirectMatch Permanent ^/category/([^/]*)/(audio|video|smell)/(.*) /category/$2/$3

The (audio|video|smell) is a list of your child categories. This rule should be above the WordPress rewrite rules.

share|improve this answer
    
Toscho, you sparked some ideas. I've got a handful cats, about 20-25. Would it be allright to set up a switch statement and go something like if the calculated url is /category/multimedia/video then overwrite that to be /category/video?. If I go with this, which hook and APIs do I need to write code for? –  Average Joe Jun 17 '12 at 4:24
    
@AverageJoe See my update for a .htaccess rule. You could also check if $term_slug in my sample PHP code matches a list (an array) of predefined terms and rewrite just these term URLs. –  toscho Jun 17 '12 at 7:53

There are 3 possible options here ...

  1. If you haven't already ... go install WordPress SEO by Yoast

This plugin allows you to set canonical links and gives you the control to remove many of the duplicated category links from being indexed by Google etc.

This plugin should solve most your problems with indexing and SEO.

  1. .htaccess and rewrite rules to stop people going to category/sub-category/post-title

see http://perishablepress.com/category/web-design/htaccess/ for lots of great ideas and code samples

  1. Install a decent XML sitemap plugin and block category and tag archives. Note ... Yoast's SEO plugin has these config settings

The benefit of 1. is that it is simple and requires no backend coding.

The benefit of 2 is that it helps you to programatically work through all your canonical url problems. However you still need 3 to stop duplicated content entry appearing

The point is ... Google doesn't penalise you for bad SEO ... if you're new to all this and the best approach is to reduce the number of duplicate content links in Google in the first place.

hope that helps?

Damien

share|improve this answer

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.