1

I'm want to redirect every category page to to URL prepended by /category. For example; I want /blog redirecting to /category/blog

Why? For some reason /blog gives to same result as /category/blog, but if you'll visit /blog the navigation structure doesn't work anymore. The next page of blogs posts will be /blog/page/2 instead of /category/blog/page/2. The first example returns an 404 page.

My permalink structure is set to /%category%/%postname%/ and on the permalink page I've set the categorybase to 'categorie', Dutch for category (so read every category above as categorie haha).

Somewhere on the internet I did found the following piece of code. Unfortunately this code leads to a redirect loop:

add_filter('term_link', 'term_link_filter', 10, 3);
function term_link_filter( $url, $term, $taxonomy ) {
    if ( is_category( 'blog' ) ) {
        $url = site_url( '/categorie/blog' );
        wp_safe_redirect( $url, 301 );
        exit;
    }

    return $url;
}

Hopefully someone will know a solution.

1 Answer 1

1

Your code is working correctly but the logic of the code is wrong. You are doing this: if we are in "blog" category, redirect to "blog" category. So, if you go to blog category you will be redirected forever. You should think carefully about what /blog url is and make the proper checking.

Anyway, when dealing with 301 redirections, I prefer to try .htaccess first. If it can be done through .htaccess, do it; you will save server resources because the redirection occurs before any PHP code is loaded.

Try this in your .htaccess:

RedirectMatch 301 ^/blog(.*)$ http://example.com/category/blog$1

The above code does this: if the url start with /blog(any-string-here), redirect to /category/blog(any-string-here) with 301 HTTP status code.

If you want to catch exactly /blog to /category/blog:

RedirectMatch 301 ^/blog$ http://example.com/category/blog
1
  • Hi thanks for your reply. I've tried to manage it using .htaccess but it didn't seem to work. After that I did contact my hosting, so I'll have to wait for there action.
    – ronnyrr
    Oct 22, 2015 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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