如何在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=302first for testing—301 redirects are cached by browsers, which can make troubleshooting a pain. Once you confirm everything works, switch toR=301. - Place these rules above the WordPress-generated
# BEGIN WordPresssection. 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 towww.main-site-new.com/about - Sub-sites (e.g.,
www.main-site-old.com/site1) load normally without redirects
- Main site pages (e.g.,
内容的提问来源于stack exchange,提问作者Thanos Manolis
相关产品推荐
相关产品推荐

