You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress子分类Permalink 404错误求助:自定义链接结构异常

Hey there, let's break down what's going on with your WordPress permalink setup and fix that annoying 404 error for your subcategory pages.

Why the 404 Happens

When you set your category base to . (a dot) and use the custom permalink structure /%category%/%postname%/, WordPress gets confused. It can't tell the difference between a subcategory archive page (like /categoryA/categoryA1/) and the prefix of a post's URL. So when you try to visit the subcategory, WordPress assumes you're looking for a post that doesn't exist—hence the 404.

And when you switched to /%category%/%category%/%postname%/, while the subcategory archive starts working, your post URLs end up with duplicate category paths (like /categoryA/categoryA1/categoryA/categoryA1/postname/), which is definitely not ideal.

Two Solid Fixes

You've got two good options depending on whether you want to keep that . category base or not.

Option 1: Adjust the Category Base (Simplest Fix)

Ditch the . category base and use something like /categories/ (or any string you prefer). Keep your custom permalink structure as /%category%/%postname%/. Here's how URLs will look:

  • Main category archive: /categories/categoryA/
  • Subcategory archive: /categories/categoryA/categoryA1/
  • Post URL: /categoryA/categoryA1/postname/

This clears up the confusion for WordPress—it can easily tell archive pages apart from posts, so no more 404s.

Option 2: Add Custom Rewrite Rules (If You Must Keep . as Category Base)

If you really want to stick with the . category base, add a custom rewrite rule to your theme's functions.php file (or a child theme's functions.php to avoid losing changes when your theme updates):

function fix_subcategory_archive_404() {
    // Fix for 2-level subcategories
    add_rewrite_rule(
        '^([^/]+)/([^/]+)/?$',
        'index.php?category_name=$matches[1]/$matches[2]',
        'top'
    );
    
    // Uncomment below if you have 3-level subcategories too
    /*
    add_rewrite_rule(
        '^([^/]+)/([^/]+)/([^/]+)/?$',
        'index.php?category_name=$matches[1]/$matches[2]/$matches[3]',
        'top'
    );
    */
}
add_action('init', 'fix_subcategory_archive_404');

After adding this code, go to your WordPress admin → Settings → Permalinks and click "Save Changes" (you don't need to edit any settings—this just refreshes WordPress's rewrite rules).

With this setup:

  • Main category /categoryA/ works fine
  • Subcategory /categoryA/categoryA1/ loads without a 404
  • Post URLs stay clean: /categoryA/categoryA1/postname/
Quick Note

If you add deeper subcategories later (like 3 or 4 levels), just extend the rewrite rule pattern to match the additional levels, similar to the commented example for 3-level categories.

内容的提问来源于stack exchange,提问作者Oliver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:37:01