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

WordPress根域名重写规则失效(子页面可正常映射)

解决方案:WordPress根域名映射落地页失效问题

问题根源

你的根域名映射失效,是因为当前重写规则匹配的是index.php路径,但根域名请求(如landing.purplepanda.be/)对应的是空路径,WordPress会触发默认规则将其解析为首页,导致跳转。

修复步骤

1. 修正重写规则匹配逻辑

修改custom_rewrite_rules函数中根域名场景的规则,将匹配^index\.php/?$改为匹配空路径^$,确保根域名请求能被正确捕获:

function custom_rewrite_rules() {
    include dirname(__FILE__).'/../domainMap.php';
    $current_host = $_SERVER['HTTP_HOST'];
    // Clear existing rules
    remove_rewrite_rules();

    // Loop through the domain map and add new rules
    foreach ($domainMap as $source_url => $target_url) {
        $slug = basename(rtrim($source_url, '/'));

        // Ensure target domain matches current request domain
        if (parse_url('https://' . $target_url, PHP_URL_HOST) === $current_host) {
            $target_path = trim(parse_url('https://' . $target_url, PHP_URL_PATH), '/');

            // Root domain case (example.com → mapped page)
            if ($target_path === '') {
                echo '<div class="message" style="display:none;"> its a root '. 'index.php?pagename=' . $slug . '</div>';
                // 匹配根域名空路径请求,优先级设为top
                add_rewrite_rule('^$', 'index.php?pagename=' . $slug, 'top');
            } else {
                // Subpage case (example.com/page → mapped page)
                add_rewrite_rule('^' . $target_path . '/?$', 'index.php?pagename=' . $slug, 'top');
            }
        }
    }

    // Flush rewrite rules to ensure they are updated
    maybe_flush_rewrite_rules();

    // Debugging: Log the current host and domain map
    error_log('Current Host: ' . $current_host);
    error_log('Domain Map: ' . print_r($domainMap, true));
}

2. 阻止Canonical跳转

WordPress的redirect_canonical函数会自动修正URL,可能导致根域名请求被强制跳转到首页。添加以下函数,在根域名映射场景下禁用该跳转:

function prevent_root_canonical_redirect() {
    include dirname(__FILE__).'/../domainMap.php';
    $current_host = $_SERVER['HTTP_HOST'];
    $request_path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
    
    // 仅处理根路径请求
    if ($request_path === '') {
        foreach ($domainMap as $source_url => $target_url) {
            $target_host = parse_url('https://' . $target_url, PHP_URL_HOST);
            $target_path = trim(parse_url('https://' . $target_url, PHP_URL_PATH), '/');
            
            // 匹配当前根域名映射规则
            if ($target_host === $current_host && $target_path === '') {
                // 移除canonical跳转动作
                remove_action('template_redirect', 'redirect_canonical');
                break;
            }
        }
    }
}
add_action('template_redirect', 'prevent_root_canonical_redirect', 0);

注意事项

  • 修改代码后,手动刷新重写规则:进入WordPress后台→设置→固定链接,直接点击「保存更改」即可触发规则刷新。
  • 检查服务器配置:如果是Apache,确保mod_rewrite模块已启用;如果是Nginx,确认配置文件中存在将所有请求转发到index.php的规则。
  • 测试前清空浏览器缓存,避免旧的跳转缓存干扰结果。

内容的提问来源于stack exchange,提问作者Gert-Jan Feijens

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 04:12:12