You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Core PHP中子域名映射至自定义域名实现方案问询

Can You Map a Custom Domain to a Subdomain Using Core PHP?

Great question! Let’s break this down clearly—since you’re working with a Core PHP app, there are a couple of ways to handle this domain mapping, but first, let’s clarify what "map" means for your use case:

Option 1: 301/302 Redirects (User is Sent to the Subdomain)

If you want visitors to abc.com to be automatically sent to company.xyz.com (with the URL changing in their browser), PHP can absolutely handle this. This is the simplest approach, and great for SEO if you want to permanently associate abc.com with your subdomain.

Just create an index.php file in the root directory of your abc.com hosting, and add this code:

<?php
// Use 301 for permanent redirects (best for SEO)
header("HTTP/1.1 301 Moved Permanently");
// Append the original request path so abc.com/about goes to company.xyz.com/about
header("Location: https://company.xyz.com" . $_SERVER['REQUEST_URI']);
exit(); // Make sure no other code runs after the redirect
?>

This will redirect every request to abc.com (including specific pages like abc.com/contact) to the corresponding page on company.xyz.com.

Option 2: Reverse Proxy (User Stays on abc.com, Content Comes from Subdomain)

If you want visitors to stay on abc.com while seeing content from company.xyz.com (URL stays the same), PHP can do this too—though it’s not the most efficient solution (server-level tools like Nginx or Apache are better for this). That said, if you’re limited to using only PHP, here’s how you can pull it off with cURL:

<?php
$targetUrl = "https://company.xyz.com" . $_SERVER['REQUEST_URI'];

// Initialize cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $targetUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true); // Capture response headers

// Execute the request
$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);

// Pass through headers to the client (skip Transfer-Encoding to avoid issues)
foreach (explode("\r\n", $headers) as $header) {
    if (!empty($header) && strpos($header, 'Transfer-Encoding') === false) {
        header($header);
    }
}

// Output the content from the subdomain
echo $body;
curl_close($ch);
?>

Note: This approach has caveats—you might run into issues with cookies, HTTPS certificates, or performance under high traffic. If you have access to your server’s config, using Nginx’s proxy_pass or Apache’s mod_proxy is a better long-term solution.

Critical Pre-Requisite

Before any of this works, you need to point abc.com’s DNS records to the server hosting your PHP script. If abc.com isn’t resolving to your server, the PHP code won’t run at all—this is a DNS setup step, not something PHP can handle.


内容的提问来源于stack exchange,提问作者Ashish Pundir

火山引擎 最新活动