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

如何为WordPress批量设置博客文章302重定向至子域名新站点

Got it, let's tackle this problem efficiently—no need to slog through 200+ manual redirects. Here are three solid, low-effort solutions tailored exactly to your needs (keeping pages on the main site, auto-redirecting only blog posts to the subdomain):

1. Use a WordPress Redirect Plugin (Easiest for Non-Technical Folks)

If you don’t want to mess with code or server files, this is your best bet. Plugins like Redirection (a widely used, reliable option) let you batch-create redirects in minutes:

  • Install and activate the plugin on your main WordPress site.
  • Head to the plugin’s settings page, then navigate to the Import/Export tab.
  • First, export your main site’s blog post URLs: Use WordPress’s built-in Tools > Export to download an XML file of just your posts (uncheck the "Pages" box).
  • Open the XML file, extract all post permalinks (look for <link> tags pointing to your main site’s posts).
  • Back in the Redirection plugin, use the Add Redirects bulk tool: Paste the list of old URLs, then set the target URL pattern to replace your main domain with blog.sitename.com (e.g., swap https://sitename.com/ for https://blog.sitename.com/).
  • Set the redirect type to 302 Temporary (as you specified).
  • Bonus: The plugin lets you add a condition to only apply redirects to the "Post" content type, so pages are automatically excluded—no accidental redirects.
2. .htaccess Rule (Fastest, Server-Level Solution)

If you have access to your site’s .htaccess file (usually in the root folder of your main site), this method is lightning-fast because it works at the server level before WordPress even loads.
First, backup your .htaccess file (critical in case something goes wrong). Then add these rules above the WordPress-generated # BEGIN WordPress section:

# Redirect only blog posts to subdomain (302 temporary)
RewriteEngine On
# Exclude specific pages (add more lines for additional pages you want to keep on main site)
RewriteCond %{REQUEST_URI} !^/about/
RewriteCond %{REQUEST_URI} !^/contact/
# Match date-based post URLs (adjust regex if your post structure is different)
# Example: Matches URLs like /2024/05/my-blog-post/
RewriteRule ^([0-9]{4}/[0-9]{2}/.+)$ https://blog.sitename.com/$1 [R=302,L]
  • If your posts use a different URL structure (e.g., /post/my-blog-post/), update the RewriteRule regex to ^post/(.+)$ instead.
  • The [R=302] flag sets the temporary redirect, and [L] tells the server to stop processing further rules once this one matches.
  • This ensures only posts get redirected—pages you exclude stay on the main site.
3. Custom WordPress Function (Code-Based, Ultra-Precise)

If you’re comfortable editing your theme’s code (use a child theme to avoid losing changes when updating your theme), this method lets you target posts directly without any plugins:
Add this code to your child theme’s functions.php file (or use a code snippet plugin):

// Redirect single blog posts to subdomain (302 temporary)
add_action('template_redirect', 'redirect_posts_to_subdomain');
function redirect_posts_to_subdomain() {
    // Check if we're on a single blog post (not a page, archive, etc.)
    if (is_single() && get_post_type() === 'post') {
        // Get the current post's permalink
        $old_url = get_permalink();
        // Replace main domain with subdomain
        $new_url = str_replace('https://sitename.com/', 'https://blog.sitename.com/', $old_url);
        // Perform 302 redirect
        wp_redirect($new_url, 302);
        exit;
    }
}
  • This code runs every time a page loads. It checks if the current request is a single blog post, swaps the domain part of the URL, and redirects immediately.
  • Pages, archives, and other content types are completely ignored—perfect for your use case.
  • Remember to replace sitename.com with your actual domain.

Quick Recommendation

  • If you’re not technical: Go with the plugin method—it’s straightforward and lets you manage redirects later if needed.
  • If you want maximum performance: Use the .htaccess rule.
  • If you want full control without plugins: Use the custom function.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:08:51