无需修改WordPress后台固定链接设置,为文章加blog/前缀并解决重定向问题
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:
- Go to Settings → Permalinks
- 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
abouttoabout-us), then visit the oldexample.com/about/URL—it should automatically redirect toexample.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

