将域重定向到不同的.php文件

时间:2014-05-21 07:47:05

标签: php linux ubuntu dns subdomain

我有一个webhoster,它不为我的DNS域提供名称别名。我有2个不同的域名。两者都应该重定向到不同的目标(例如/index.php,/abc/index2.php),但是在同一个根服务器上。

我如何将这两个域与一个服务器与PHP或工具(ubuntu)一起使用?

感谢。

2 个答案:

答案 0 :(得分:1)

您应检查请求的主机标头HTTP_HOST,以了解请求所在的域。

if (strpos($_SERVER["HTTP_HOST"], "domaina.com") !== false) {
  header('Location: '.$urldomaina);
}
else if (strpos($_SERVER["HTTP_HOST"], "domainb.com") !== false)
{
    header('Location: '.$urldomainb);
}

答案 1 :(得分:1)

如果您的网站空间上有第一个域(domain.com),请将另一个域(domain.org)映射到同一个Webspace(这称为Domain-Alias)。

现在,您可以使用.htaccess(mod_rewrite)重定向每个域:

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^domain\.com$ 
RewriteRule ^(.*)$ http://redirect.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} !^domain\.org$ 
RewriteRule ^(.*)$ http://redirect.org/$1 [L,R=301]