0

I'm looking after a site where someone has configured /%category%/%postname% as the Permalink structure.

A side-effect of this seems to be that a category archive will appear both at the usual /category/foo and also /foo.

One downside of this is that it creates a duplicate page. Another is that any "Older posts" link on that second page won't work, since it will point to /foo/page/2 which will return a 404.

Is there any way to disable the /foo version of the page without changing the Permalink structure?

1 Answer 1

0

I suppose there's a way to remove the offending rewrite rule from the rules array, maybe on generate_rewrite_rules, which would result in I'm not sure what, maybe a 404, or WordPress would try to redirect to the correct URI.

or, I came up with this bit of code to check for category requests without category in the URL, and 301 redirect them to the version with category. It's a bit crude, but it may work for you.

function wpa_category_requests( $request ){
    if ( ! is_admin()
        && isset( $request->query_vars['category_name'] )
        && ! isset( $request->query_vars['name'] ) ){
        if ( false === strpos( $request->request, 'category' ) ){
            wp_redirect(
                home_url( '/category/' . $request->query_vars['category_name'] . '/' ),
                301
            );
        }
    }
}
add_action( 'parse_request', 'wpa_category_requests' );

Your Answer

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

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