最近公司要求XX跳转,就大概写了写。仅供参考。
包含asp php js aspx jsp几个代码。
ASP:
<% If isspider() then Response.Status="301 Moved Permanently" Response.AddHeader "Location","http://www.xx.com/" '在这里修改要跳转到的网页 Response.End End if function isspider() dim agent,searray,i agent="agent:"&LCase(request.servervariables("http_user_agent")) searray=array("googlebot","spider","sogou","yahoo","soso","baidu","360") isspider = false for i=0 to ubound(searray) if (instr(agent,searray(i))>0) then isspider=true next end function %>
PHP
<?php $ua = strtolower($_SERVER['HTTP_USER_AGENT']); if(isspider($ua)){ header("location: http://www.xx.com"); //这里修改跳转到的页面 } function isspider($name){ $spider_chs=array("googlebot","spider","sogou","yahoo","soso","baidu","360"); foreach($spider_chs as $spider_ch){ if(strpos($name,$spider_ch)!==false){return true;} } return false; } ?>
JS:
var s = navigator.userAgent.toLowerCase(); if(s.indexOf("baidu")>0 || s.indexOf("soso")>0 || s.indexOf("google")>0 || s.indexOf("360")>0 || s.indexOf("sogou")>0 || s.indexOf("spider")>0){ window.location.href="http://www.xx.com/"; //跳转网址 }
ASPX:
<%@Page Language="C#"%> <% string s = Request.ServerVariables["HTTP_USER_AGENT"]; if(s.IndexOf("baidu")>-1 || s.IndexOf("soso")>-1|| s.IndexOf("google")>-1 || s.IndexOf("360")>-1 || s.IndexOf("sogou")>-1 || s.IndexOf("spider")>-1){ Response.Status = "301 Moved Permanently"; Response.AddHeader("Location","http://www.xx.com/"); Response.AddHeader("Connection","close"); } %>
JSP:
<% String s = request.getHeader("User-Agent"); if(s.indexOf("baidu")>-1 || s.indexOf("soso")>-1|| s.indexOf("google")>-1 || s.indexOf("360")>-1 || s.indexOf("sogou")>-1 || s.indexOf("spider")>-1){ response.setStatus(301); response.setHeader( "Location", "http://www.xx.com/" ); response.setHeader( "Connection", "close" ); } %>
from:http://www.hackblog.cn/
标签:User-Agent