虚拟主机通过域名跳转目录的实现方法

更新于 2018-03-20

案例一:asp语言,asp语言对应的是windows主机,目前实测可以支持的方案代码如下

 

1、这里用到了ASP中的case选择语句,根据servervariables("HTTP_HOST")获取的主机HTTP头,也就是域名,来判断需要跳转到哪些目录中,特别适合于二级域名的网站使用

<%
host=lcase(request.servervariables("HTTP_HOST"))  ‘取得HTTP输入的值并付值到HTOST中
select CASE host  ‘设置跳转条件’
CASE "a.ethnicity.cn" ‘如果HOST的值是 a.ethnicity.c 就选择case"a.ethnicity.c"的命令访问/a目录下站点’
response.redirect "/a/"
CASE "b.ethnicity.cn"
response.redirect "/b/"
case "c.ethnicity.cn"  
Server.Transfer("b/default.asp")  ‘如果不在上述特定范围’

CASE ELSE
response.redirect "/else/"   ‘转到else目录’
END select
%>

 

案例二:php语言,php语言可以对应的是轻云服务器linux的php或者普通的虚拟主机的linux系统

 

1、PHP 跳转代码实现一个网站空间绑定多个域名,建立多个网站

switch ($_SERVER["HTTP_HOST"])
{
case "a.ethnicity.cn":
header("location:a/");
break;
case "b.ethnicity.cn":
header("location:b/");
break;
case "c.ethnicity.cn":
header("location:c/");
break;
}
?>

 

2、key-value的模式

$domain_route = array(
        ‘a.ethnicity.cn’ => ‘a/‘,
        ‘b.ethnicity.cn’ => ‘b/‘,
        ‘c.ethnicity.cn’ => ‘c/‘,
        ‘d.ethnicity.cn’ => ‘main.php’,
);
$domain = $_SERVER[‘HTTP_HOST’];
$target_url = $domain_route[$domain];
header("location:{$target_url}");
?>

电话咨询