WordPress多站点独立域名批量重定向至主站对应落地页技术问询
Got it, let's break this down—handling hundreds of domain redirects manually is a nightmare, so generating rules programmatically is exactly the right move. Here's a step-by-step guide tailored to your WordPress multisite setup:
Before writing any code, you need a clear, structured list of which domain maps to which main site landing page. For example:
domain1.com→https://your-main-domain.com/domain1-landingdomain2.net→https://your-main-domain.com/domain2-promotion
Store this mapping in a machine-readable format like a JSON file or CSV—this will be the source of truth for your script. Example JSON structure (domain_mappings.json):
{ "domain1.com": "/domain1-landing", "domain2.net": "/domain2-promotion", "domain3.org": "/special-offer-page" }
Use any language you’re comfortable with (Python, PHP, Bash—whatever) to loop through your mapping and spit out valid Apache rewrite rules. Below is a Python example, since it’s straightforward for this kind of task:
import json import re # Replace with your actual main domain MAIN_DOMAIN = "your-main-domain.com" # Load the domain mapping with open("domain_mappings.json", "r") as f: domain_map = json.load(f) # Build the redirect rules redirect_rules = [] # Only add RewriteEngine On if your main .htaccess doesn't already have it redirect_rules.append("RewriteEngine On") # Rule to exclude your main domain (so the admin backend isn't redirected) redirect_rules.append(f'RewriteCond %{{HTTP_HOST}} !^{re.escape(MAIN_DOMAIN)}$ [NC]') # Generate rules for each domain for source_domain, target_path in domain_map.items(): # Match the source domain (case-insensitive) redirect_rules.append(f'RewriteCond %{{HTTP_HOST}} ^{re.escape(source_domain)}$ [NC]') # 301 permanent redirect (use 302 for temporary testing!) redirect_rules.append(f'RewriteRule ^(.*)$ https://{MAIN_DOMAIN}{target_path} [R=301,L]') # Save rules to a file for easy copying with open("generated_redirects.txt", "w") as f: f.write("\n".join(redirect_rules))
Key Rule Details:
[NC]: Makes the domain match case-insensitive (soDomain1.comworks the same asdomain1.com).[R=301]: Permanent redirect, which is best for SEO—test withR=302first to avoid browser caching wrong redirects.[L]: Tells Apache to stop processing further rules once this one matches, preventing conflicts.
WordPress multisite already has its own rewrite rules in the root .htaccess—you need to place your generated redirect rules BEFORE the default WordPress rules. This ensures redirects take priority over multisite routing.
Your final .htaccess should look something like this:
# Generated Redirect Rules (paste your rules here) RewriteEngine On RewriteCond %{HTTP_HOST} !^your-main-domain.com$ [NC] RewriteCond %{HTTP_HOST} ^domain1.com$ [NC] RewriteRule ^(.*)$ https://your-main-domain.com/domain1-landing [R=301,L] RewriteCond %{HTTP_HOST} ^domain2.net$ [NC] RewriteRule ^(.*)$ https://your-main-domain.com/domain2-promotion [R=301,L] ... # WordPress Multisite Default Rules (leave this section untouched!) RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] # Add trailing slash to /wp-admin RewriteRule ^wp-admin$ wp-admin/ [R=301,L] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] RewriteRule ^(wp-(content|admin|includes).*) $1 [L] RewriteRule ^(.*\.php)$ $1 [L] RewriteRule . index.php [L]
- Verify
mod_rewriteis enabled: Runapache2ctl -M | grep rewriteon your server—you should seerewrite_module (shared). - Test with temporary redirects first: Use
R=302instead of301during testing to avoid browser caching issues. Usecurl -I http://domain1.comto check the redirect location. - Avoid duplicate
RewriteEngine On: If your main.htaccessalready has this line, remove it from your generated rules to prevent errors. - Handle subdomains (if needed): If you want to redirect all subdomains of a source domain, adjust the condition to
RewriteCond %{HTTP_HOST} ^(.*\.)?domain1.com$ [NC].
内容的提问来源于stack exchange,提问作者cschaefer

