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

如何实现双后缀域名共享同一WordPress站点副本?

Yes, You Can Serve Identical Content on Both Domains Without Duplicating WordPress

Absolutely, this is achievable with a few configuration changes—no need to maintain two separate WordPress installations. Here's how to set it up step by step:

1. Update Your Virtual Host Configuration

First, you'll need to adjust your web server (Apache/Nginx) settings so both mydomain.com and mydomain.org point to the same WordPress document root, and remove any existing redirect rules for mydomain.com.

For Apache:

Edit your virtual host files to set the same DocumentRoot for both domains. Example:

# Virtual Host for mydomain.org
<VirtualHost *:80>
    ServerName mydomain.org
    ServerAlias www.mydomain.org
    DocumentRoot /var/www/wordpress
    # Include standard WordPress rewrite rules
    <Directory /var/www/wordpress>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

# Virtual Host for mydomain.com (no redirect)
<VirtualHost *:80>
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot /var/www/wordpress
    <Directory /var/www/wordpress>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Don't forget to add SSL configurations for both domains (using Let's Encrypt or your certificate provider) to serve HTTPS content securely.

For Nginx:

Update your server blocks to point both domains to the same root directory:

server {
    listen 80;
    server_name mydomain.org www.mydomain.org;
    root /var/www/wordpress;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    }
}

server {
    listen 80;
    server_name mydomain.com www.mydomain.com;
    root /var/www/wordpress;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    }
}

Again, add SSL settings for both domains to enforce HTTPS.

2. Configure WordPress to Recognize Both Domains

WordPress is hardcoded to use the domain you set in its admin settings by default. To make it dynamically adapt to the visitor's domain, add these lines to your wp-config.php file (right above the /* That's all, stop editing! Happy publishing. */ line):

// Dynamically set site URL based on the visitor's domain
define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);

This ensures WordPress generates links using the domain the visitor is currently on, instead of a fixed one.

If you already have media files or content links pointing to mydomain.org, you'll want to make sure those update dynamically too. Add this code to your theme's functions.php file or a custom plugin:

// Ensure all generated URLs use the current domain
add_filter('home_url', 'dynamic_home_url', 10, 4);
function dynamic_home_url($url, $path, $scheme, $blog_id) {
    return esc_url_raw(sprintf(
        '%s://%s/%s',
        $scheme,
        $_SERVER['HTTP_HOST'],
        ltrim($path, '/')
    ));
}

// Update existing attachment URLs in content
add_filter('the_content', 'swap_domain_in_content');
function swap_domain_in_content($content) {
    $old_domain = 'mydomain.org';
    $new_domain = $_SERVER['HTTP_HOST'];
    return str_replace($old_domain, $new_domain, $content);
}

4. Avoid SEO Duplicate Content Issues

Since both domains will serve identical content, you need to tell search engines which domain is your primary one to avoid duplicate content penalties. Add a canonical tag to all pages by inserting this code into your theme's functions.php:

add_action('wp_head', 'add_canonical_tag');
function add_canonical_tag() {
    // Replace with your preferred primary domain
    $primary_domain = 'https://mydomain.org';
    echo '<link rel="canonical" href="' . esc_url($primary_domain . $_SERVER['REQUEST_URI']) . '" />' . "\n";
}

Alternatively, use an SEO plugin like Yoast SEO to set the canonical domain through its settings.

Final Checks

  • Test both domains in a private browsing window to ensure they load the same content without redirects.
  • Verify that all links (navigation, media, posts) use the current domain instead of the old fixed one.
  • Make sure SSL certificates are working correctly for both domains to avoid security warnings.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:36:21