Wordpress - Including category-base in a post permalink results in 404

I returned to this problem recently and I've found the solution finally! It may or may not work for you - there are two possible cases:

  1. If some posts on your site are placed under parent categories and some - in subcategories (child categories), or the categories have different nesting levels (some parent categories have only subcategories while others - sub-subcategories), then there is no solution. It's impossible to distinguish news/category/post-name/ from news/category/subcategory/ with regex in rewrite rules.

  2. If you have fixed nesting level for all categories and post only in the last-level subcategories, then you are in luck! There is quite simple solution:

The reason of those 404 pages with news both in category base and custom permalink structure is that the category base internal rewrite rule takes the precedence over the permalink structure rules. So news/category/subcategory/postname/ is interpreted as index.php?category_name=category/subcategory/postname and, obviously, WordPress can't find such category, returning 404 page.

What you need to do is to add new rewrite rule on top of the rules list. Place this code in your functions.php or wherever you think it's appropriate:

add_action( 'init', 'wpa58471_category_base' );
function wpa58471_category_base() {
    // Remember to flush the rules once manually after you added this code!
    add_rewrite_rule(
        // The regex to match the incoming URL
        'news/([^/]+)/([^/]+)/([^/]+)(/[0-9]+)?/?$',
        // The resulting internal URL
        'index.php?category_name=$matches[1]/$matches[2]&name=$matches[3]&paged=$matches[4]',
        // Add the rule to the top of the rewrite list
        'top' );
}

and then update permalink structure from your WordPress General Options page. The code above is for two-level nesting, if you have more or less nested category structure, then edit the regex and resulting URL accordingly.

P.S. thanks to contributors of these useful answers: help with add_rewrite_rule and tool to analyze rewrite rules.


You only need to add "/." after category base:

Category base: news/. (/. is required as it prevents 404s for post).

This worked perfectly for me :)

EDIT: This field will strip trailing or leading slashes unless you include the "."