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.

I have static front-page and separate page /news/ as the Posts page. I would like to have the single-post URLs like this:

www.example.com/news/categoryname/subcategory/postname/

and for the category archives:

www.example.com/news/categoryname/

What I've tried already:

  • If I add news as my category base and /news/%category%/%postname%/ as the custom permalink structure, I get the desired URLs but also 404 error for single posts (both with news/ in URL and without it). All other pages (e.g. category archives, pages etc.) are accessible.

  • If I leave category-base empty and permalinks as /news/%category%/%postname%/, there are no 404 errors, but category URLs additionally contain the default category slug, like so: www.example.com/news/category/categoryname/

  • If I use plugins to remove the default category base or just set category base to . it's removed together with the news part

  • If I set permalinks as /%category%/%postname%/ and add only the category base news, there are also no 404 errors, but links to posts don't start with news: www.example.com/categoryname/subcategory/postname/

There are some similar unanswered questions: Posts not showing with custom categorybase and subcategories and wordpress permalinks tweeks

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
While there is no solution using regex in rewrite rules, there still is a way to have this functionality: wordpress.stackexchange.com/questions/98083/… –  Jeremy May 2 '13 at 17:09

Custom permalink structure: /media/%category%/%postname%/ Category base: .

Worked for me, WordPress 3.4.1, without any related plugins.

Update: Well, later I've noticed that wp_list_categories outputs links without "media" in the URL. But posts are accessible with the desired URL structure, and categories archives is in the right place. Now I'm looking into changing wp_list_categories behavior.

share|improve this answer
    
Thanks for the reply, but there are 2 problems: I can't access categories archives (both with media and without, using the same version 3.4.1), and actual generated links contain /./ in their addresses. –  semyonfilippov Aug 23 '12 at 6:21
    
That's strange. I guess there maybe 2 reasons: 1. Some of your plugins takes side effects. 2. My WordPress initial setup is somehow different. I'm using qwordpress.ru with immediate upgrade to current WP version. And in that case the first thing I was working on is the categories structure and URLs, so setup was quite pure. –  Lev Eidinov Aug 24 '12 at 6:55

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.