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

如何在WordPress多站点中仅将主站301重定向至新域名

Fix: Redirect Only the Main WordPress Network Site to a New Domain

The issue with your initial Redirect 301 / http://www.main-site-new.com/ rule is that it’s a broad catch-all—every request under www.main-site-old.com (including sub-sites like /site1) gets redirected. We need to use targeted rewrite conditions to exclude sub-sites while redirecting only the main site.

Apache Server (Using .htaccess)

This is the most common setup for WordPress. Add these rules before the default WordPress Network rewrite rules in your root .htaccess file:

RewriteEngine On

# Target only the old main domain
RewriteCond %{HTTP_HOST} ^www\.main-site-old\.com$ [NC]

# Exclude your sub-sites—add one line per sub-site
RewriteCond %{REQUEST_URI} !^/site1/ [NC]
RewriteCond %{REQUEST_URI} !^/site2/ [NC]
# If all your sub-sites start with "/site", you can use this single line instead:
# RewriteCond %{REQUEST_URI} !^/site [NC]

# Redirect to the new domain, preserving the original path (e.g., /about → new-domain.com/about)
RewriteRule ^(.*)$ https://www.main-site-new.com/$1 [R=302,L]

Key Notes:

  • Use R=302 first for testing—301 redirects are cached by browsers, which can make troubleshooting a pain. Once you confirm everything works, switch to R=301.
  • Place these rules above the WordPress-generated # BEGIN WordPress section. WordPress’s own rewrite rules handle sub-site routing, so we want to check for redirects first.
  • If you add more sub-sites later, just add another RewriteCond %{REQUEST_URI} !^/new-site/ [NC] line.

Nginx Server

If you’re using Nginx, update your server block configuration for www.main-site-old.com like this:

server {
    listen 80;
    listen 443 ssl;
    server_name www.main-site-old.com;

    # Redirect main site only—exclude any path starting with /site
    if ($request_uri !~ ^/site) {
        return 302 https://www.main-site-new.com$request_uri;
    }

    # WordPress Network routing rules
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # Add your other Nginx configs (SSL, PHP handling, etc.) here
}

Testing Tips:

  • Clear your browser cache before testing, or use incognito mode to avoid cached redirects.
  • Verify:
    • Main site pages (e.g., www.main-site-old.com/about) redirect to www.main-site-new.com/about
    • Sub-sites (e.g., www.main-site-old.com/site1) load normally without redirects

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:43:17