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

无需修改WordPress后台固定链接设置,为文章加blog/前缀并解决重定向问题

Solution for Adding "blog/" Prefix to Post URLs Without Breaking Page Slug Redirects

Hey there! I’ve run into this exact issue before—modifying the global permalink structure to blog/%postname% does add the prefix to posts, but it messes up WordPress’s automatic slug redirects for pages. Here’s a clean, code-based fix that keeps post URLs prefixed with blog/ while preserving page redirect functionality:

Step 1: Customize Post Type Rewrite Rules

Instead of changing the global permalink structure, we’ll override the rewrite rules specifically for the post type. This leaves page permalinks untouched, so their slug redirects work as expected.

Add this code to your theme’s functions.php file or a custom plugin:

// Modify post type rewrite rules to add "blog/" prefix
add_filter('register_post_type_args', 'custom_post_rewrite_prefix', 10, 2);
function custom_post_rewrite_prefix($args, $post_type) {
    if ($post_type === 'post') {
        $args['rewrite'] = array(
            'slug' => 'blog',          // Adds the "blog/" prefix
            'with_front' => false,     // Avoids conflicts with global permalink front settings
            'feeds' => true,           // Keep RSS feeds working
            'pages' => true            // Supports pagination for archives
        );
    }
    return $args;
}

Step 2: Redirect Old Post URLs to New Prefixed URLs

If you already have existing posts with the old /%postname% structure, add this code to redirect them to the new blog/%postname% URLs (permanent 301 redirect for SEO):

// Redirect old post URLs without "blog/" prefix to new ones
add_action('template_redirect', 'redirect_old_post_permalinks');
function redirect_old_post_permalinks() {
    global $wp, $post;

    // Check if we're on a single post and the URL doesn't have the "blog/" prefix
    if (is_singular('post') && strpos($wp->request, 'blog/') === false) {
        $new_url = home_url('/blog/' . $post->post_name . '/');
        wp_safe_redirect($new_url, 301);
        exit;
    }
}

Step 3: Refresh Rewrite Rules

After adding the code, log into your WordPress admin:

  1. Go to Settings → Permalinks
  2. Click Save Changes (you don’t need to modify any settings—this just flushes the rewrite rules to apply our new configuration)

Step 4: Test the Setup

  • Post URLs: Visit an existing post—its URL should now start with blog/, and old post URLs should redirect to the new ones.
  • Page Slug Redirects: Edit a page’s slug (e.g., change about to about-us), then visit the old example.com/about/ URL—it should automatically redirect to example.com/about-us/ as expected.

Bonus: Add "blog/" Prefix to Category Archives (Optional)

If you want category URLs to also include the blog/ prefix (e.g., blog/category/tech/), add this code:

// Modify category taxonomy rewrite rules
add_filter('register_taxonomy_args', 'custom_category_rewrite_prefix', 10, 2);
function custom_category_rewrite_prefix($args, $taxonomy) {
    if ($taxonomy === 'category') {
        $args['rewrite'] = array(
            'slug' => 'blog/category',
            'with_front' => false,
            'hierarchical' => true    // Keeps parent/child category URLs working
        );
    }
    return $args;
}

Don’t forget to save permalinks again after adding this.

Why This Works

When you modify the global permalink structure to blog/%postname%, WordPress applies this rule to both posts and pages, which disrupts the canonical redirect logic for pages. By targeting only the post type’s rewrite rules, we keep page permalinks in their default structure, so WordPress’s built-in slug redirect system continues to function correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:04:02