ASP/PHP实现主机头多域名转向的方法

[ 2004-09-08 18:24:22 | Author: zhenhua ]
Font Size: Large | Medium | Small
ASP:
1.使用CASE语句,缺点:同一域名即@和www需要两次循环,代码显重复
<%
host=lcase(request.servervariables("HTTP_HOST"))
select CASE host
CASE "www.abc.com"
 response.redirect "index.htm"
CASE "abc.com"
 response.redirect "index.htm"

CASE ELSE
 response.redirect "other.htm"
END select
%>
--------------------------
<%if instr(Request.ServerVariables
("SERVER_NAME"),"kekexi.com")>0 then
response.redirect "index.asp"
else if instr(Request.ServerVariables
("SERVER_NAME"),"4668.com")>0 then
response.redirect "x/index.asp"
else if instr(Request.ServerVariables
("SERVER_NAME"),"web315.com")>0 thenr
esponse.redirect "index3.asp"
end if
end if

-----------------------
<%if Request.ServerVariables("SERVER_NAME")="www.yyjs.com" then
response.redirect "zkj"
else
response.redirect "i.htm"
end if%>

----------------------------
<%if Request.ServerVariables("SERVER_NAME")="www.yyjs.com" then
response.redirect "index1.asp"
else if Request.ServerVariables("SERVER_NAME")="www.ce.org.cn" then
response.redirect "index2.asp"
else if Request.ServerVariables("SERVER_NAME")="www.163.com" then
response.redirect "index3.asp"
end if
end if
end if%>
 
 

2.为解决上述问题,可以加条件判断加上www. 这样就可以自动识别www前缀:
<%
dim domain
domain=request.ServerVariables("Server_Name")
if instr(domain,"www.") then domain=right(domain,len(domain)-4)
if domain="abc.com" then
 response.redirect "1/"
elseif domain="123.com" then
 response.redirect "2/"
else
 response.redirect "3/"
end if
%>

3.

====================================
PHP:
1.多个
<?php
$domain_net="abc.com";
$dot_net_url="目录1/";
$dot_com_url="目录2/";
if(($HTTP_HOST=="$domain_net")or($HTTP_HOST=="www.$domain_net"))
{
Header("Location: $dot_net_url");
}
else
{
Header("Location: $dot_com_url");
}
?>
<?php
switch ($_SERVER["HTTP_HOST"]) {
case "ychon.org":
header("location:bbs");
break;
default:
header("location:news");
break;
}
?>

2.单个
<?
Header("Location: [url]http://www.ychon.org");
?>
或:
<?
echo "<meta http-equiv=refresh content='0; url=http://www.ychon.org'>";
?>
还可以这样用:
<?
header("refresh:3;url=http://web.com");
?>
注意:使用Header函数时必须网页未产生任何输出,此时尤其要注意空格的问题。即必须放在网页最开始处。

====================================
Java Script :
<script>try { if( self.location == "http://玉米一/" ) {
top.location.href = "http://玉米一/目录";
}
else if( self.location == "http://玉米二/" ) {
top.location.href = "http://玉米二/目录";
}
else if( self.location == "http://玉米三/" ) {
top.location.href = "http://玉米三/目录";
}
else if( self.location == "http://玉米四/" ) {
top.location.href = "http://玉米四/目录";
}
else { document.write ("错误的访问地址") } } catch(e) { }</script>


-------------------------------------
<script language="javascript">
switch (window.location.hostname) {
case "test" ://确定玉米为TEST
window.location.pathname="1"//跳转到1目录
break;
case "127.0.0.1" ://确定玉米为127.0.0.1
window.location.pathname="2"//跳转到2目录
break;
//依次类推写下去
default ://如果没有找到该玉米
window.location.pathname="3"//跳转到3目录
}
</script>


location对象有以下几个属性:

hash 设置或获取 href 属性中在井号“#”后面的分段。
host 设置或获取 location 或 URL 的 hostname 和 port 号码。
hostname 设置或获取 location 或 URL 的主机名称部分。
href 设置或获取整个 URL 为字符串。
pathname 设置或获取对象指定的文件名或路径。
port 设置或获取与 URL 关联的端口号码。
protocol 设置或获取 URL 的协议部分。
search 设置或获取 href 属性中跟在问号后面的部分。
Comments Feed Comments Feed: http://www.zhenhua.org/feed.asp?q=comment&id=137
UTF-8 Encoding Trackback URL: http://www.zhenhua.org/trackback.asp?id=137

There is no comment on this article.

If you feel this site you find this information helpful, please click on the donation, which is voluntary,Thank you.
Post Comment
Smilies
[arrow] [biggrin] [confused] [cool]
[cry] [eek] [evil] [exclaim]
[frown] [idea] [lol] [mad]
[mrgreen] [neutral] [question] [razz]
[redface] [rolleyes] [sad] [smile]
[surprised] [twisted] [wink]
Enable UBB Codes
Auto Convert URL
Show Smilies
Hidden Comment
Username:   Password:   Register Now?
Security Code * Please Enter the Security Code