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

多域名映射至同一WordPress站点且保留原URL的实现咨询

实现多域名无跳转访问同一WordPress站点

这个需求完全可行,核心是让WordPress适配当前访问的域名,同时服务器接收多个域名的请求并指向同一站点目录,具体步骤如下:

1. 域名解析配置

将abc.com、def.com、ghi.com三个域名的A记录(或CNAME记录)指向你的服务器公网IP,确保所有域名的请求都能到达同一服务器。如果使用HTTPS,需要为三个域名申请多域名SSL证书(或通配符证书)。

2. 修改WordPress配置(wp-config.php)

在wp-config.php文件开头添加以下代码,动态设置站点URL和主页URL,让WordPress自动适配当前访问的域名,避免硬编码主域名导致的跳转或链接错误:

// 动态适配当前访问域名
if (isset($_SERVER['HTTP_HOST'])) {
    $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://';
    define('WP_HOME', $protocol . $_SERVER['HTTP_HOST']);
    define('WP_SITEURL', $protocol . $_SERVER['HTTP_HOST']);
}

3. 服务器配置(Nginx/Apache)

Nginx 配置示例

修改Nginx站点配置文件,将三个域名添加到server_name中,确保请求转发到同一WordPress目录:

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    # 同时绑定三个域名
    server_name abc.com def.com ghi.com;

    # SSL证书配置(替换为你的多域名证书路径)
    ssl_certificate /etc/nginx/ssl/your-cert.pem;
    ssl_certificate_key /etc/nginx/ssl/your-key.pem;

    # WordPress站点根目录
    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.1-fpm.sock; # 根据你的PHP版本调整
    }
}

配置完成后重启Nginx:sudo systemctl restart nginx

Apache 配置示例

修改Apache虚拟主机配置,用ServerAlias添加额外域名,指向同一站点目录:

<VirtualHost *:80>
    ServerName abc.com
    ServerAlias def.com ghi.com
    DocumentRoot /var/www/wordpress

    <Directory /var/www/wordpress>
        AllowOverride All
        Require all granted
    </Directory>

    # 可选:HTTP强制跳转HTTPS
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>

<VirtualHost *:443>
    ServerName abc.com
    ServerAlias def.com ghi.com
    DocumentRoot /var/www/wordpress

    SSLEngine On
    SSLCertificateFile /etc/apache2/ssl/your-cert.pem
    SSLCertificateKeyFile /etc/apache2/ssl/your-key.pem

    <Directory /var/www/wordpress>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

确保Apache启用了mod_rewrite和mod_ssl模块,然后重启Apache:sudo systemctl restart apache2

同时,WordPress根目录下的.htaccess要保持默认重写规则:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

4. 修复现有内容中的硬编码链接

如果之前的文章、页面中存在硬编码的abc.com链接,可使用WP CLI命令批量替换(先测试再执行):

# 先执行测试,确认替换内容正确
wp search-replace 'https://abc.com' '' --all-tables --dry-run
# 测试无误后,去掉--dry-run执行实际替换
wp search-replace 'https://abc.com' '' --all-tables

5. 测试验证

分别访问abc.com/page、def.com/page、ghi.com/page,检查:

  • URL是否保持原域名不变,无跳转
  • 页面内容与主站点一致
  • 页面内的链接、图片等资源都使用当前访问的域名

内容的提问来源于stack exchange,提问作者Pete Harrison

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 06:25:23