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

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:

1. First, Lock Down Your Redirect Mapping

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-landing
  • domain2.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"
}
2. Write a Script to Generate .htaccess Rules

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 (so Domain1.com works the same as domain1.com).
  • [R=301]: Permanent redirect, which is best for SEO—test with R=302 first to avoid browser caching wrong redirects.
  • [L]: Tells Apache to stop processing further rules once this one matches, preventing conflicts.
3. Merge Rules Into Your Main .htaccess

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]
4. Critical Checks & Testing
  • Verify mod_rewrite is enabled: Run apache2ctl -M | grep rewrite on your server—you should see rewrite_module (shared).
  • Test with temporary redirects first: Use R=302 instead of 301 during testing to avoid browser caching issues. Use curl -I http://domain1.com to check the redirect location.
  • Avoid duplicate RewriteEngine On: If your main .htaccess already 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:16:07