在前人的一些代码修改之下,自己弄出来了一个这样的跳转方案,可能不是很完美,不过也花了不少时间总算还是实现了需求,以我现在的js能力只能做到这样了,改进可能要到以后了。看代码吧,直接用例子说话,一边解释最好。(现在觉得代码有时比说话简洁,呵呵)

  先把下面代码另存为js文件,然后可以放在网页的任何位置,当然不影响网页加载速度优先。

 

  1. var surl=document.location.href+"&from="+from;//自定义的一个来源 可以不要 
  2. var s=document.referrer; 
  3. /***** 
  4. 思路大概是: 
  5.  获取从百度来的关键词 用我们自己的关键词匹配 匹配对了 跳转到指定网页 
  6.  跳转到一个php文件的好处 是用表单做跳转 对seo影响较小 而且还可以在php里面加些自定义判断功能 
  7.  
  8. 其中有用到 站长工具(http://tool.chinaz.com/)的 url编码解码  大小写转换 
  9.  
  10. js的  .test()  函数  是看能是否字符串匹配成功 
  11. *****/ 
  12. var re=/((wd|q|w|p|query)(=)([%A-Z0-9]*)(%E9%A3%8E%E6%B9%BF))/;  //风湿 url编码 格式为utf-8的 
  13. var re1=/((wd|q|w|p|query)(=)([%A-Z0-9]*)(%E9%AA%A8%E8%B4%A8%E5%A2%9E%E7%94%9F))/;  //骨质增生 url编码 
  14. var re2=/((wd|q|w|p|query)(=)([%A-Z0-9]*)(%E9%A2%88%E6%A4%8E%E7%97%85))/;  //颈椎病 url编码 
  15. var re3=/((wd|q|w|p|query)(=)([%A-Z0-9]*)(%E5%BC%BA%E7%9B%B4%E6%80%A7%E8%84%8A%E6%9F%B1%E7%82%8E))/;  //强直性脊柱炎 url编码 
  16. var re4=/((wd|q|w|p|query)(=)([%A-Z0-9]*)(%E7%97%9B%E9%A3%8E))/;  //痛风 url编码 
  17. var re5=/((wd|q|w|p|query)(=)([%A-Z0-9]*)(%E8%85%B0%E6%A4%8E%E7%97%85))/;  //腰椎病 url编码 
  18. var re6=/((wd|q|w|p|query)(=)([%A-Z0-9]*)(%E8%82%A9%E5%91%A8%E7%82%8E))/;  //肩周炎 url编码 
  19. var re7=/((wd|q|w|p|query)(=)([%A-Z0-9]*)(%E5%93%81%E7%89%8C))/;  //品牌 url编码 
  20.  
  21. var curl=window.location.href; 
  22. if(re.test(s)) 
  23.     var iii = decodeURI('%E9%A3%8E%E6%B9%BF'); 
  24.     yh_turn('http://www.120xbk.com/swt/tiaozhuan.php?a='+iii+'&from='+from,curl,1);  //get传值 到tiaozhuan.php 
  25. else if(re1.test(s)) 
  26.     var iii = decodeURI('%E9%AA%A8%E8%B4%A8%E5%A2%9E%E7%94%9F'); 
  27.     yh_turn('http://www.120xbk.com/swt/tiaozhuan.php?a='+iii+'&from='+from,curl,1);  
  28. else if(re2.test(s)) 
  29.     var iii = decodeURI('%E9%A2%88%E6%A4%8E%E7%97%85'); 
  30.     yh_turn('http://www.120xbk.com/swt/tiaozhuan.php?a='+iii+'&from='+from,curl,1);  
  31. else if(re3.test(s)) 
  32.     var iii = decodeURI('%E5%BC%BA%E7%9B%B4%E6%80%A7%E8%84%8A%E6%9F%B1%E7%82%8E'); 
  33.     yh_turn('http://www.120xbk.com/swt/tiaozhuan.php?a='+iii+'&from='+from,curl,1);  
  34. else if(re4.test(s)) 
  35.     var iii = decodeURI('%E7%97%9B%E9%A3%8E'); 
  36.     yh_turn('http://www.120xbk.com/swt/tiaozhuan.php?a='+iii+'&from='+from,curl,1);  
  37. else if(re5.test(s)) 
  38.     var iii = decodeURI('%E8%85%B0%E6%A4%8E%E7%97%85'); 
  39.     yh_turn('http://www.120xbk.com/swt/tiaozhuan.php?a='+iii+'&from='+from,curl,1);  
  40. else if(re6.test(s)) 
  41.     var iii = decodeURI('%E8%82%A9%E5%91%A8%E7%82%8E'); 
  42.     yh_turn('http://www.120xbk.com/swt/tiaozhuan.php?a='+iii+'&from='+from,curl,1);  
  43. else if(re7.test(s)) 
  44.     var iii = decodeURI('%E5%93%81%E7%89%8C'); 
  45.     yh_turn('http://www.120xbk.com/swt/tiaozhuan.php?a='+iii+'&from='+from,curl,1);  
  46. else 
  47. {  
  48.     yh_turn('http://www.120xbk.com/swt/tiaozhuan.php',surl,1); 
  49.  
  50. //模拟提交表单跳转 
  51. function yh_turn(url,domain,from) 
  52.     function isURL(url){ 
  53.         return(/^(http(s?))\:\/\/([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?([\/\?]\S*)?$/.test(url)); 
  54.     } 
  55.      
  56.     if(!isURL(url))return false
  57.     function doSubmit() 
  58.     { 
  59.         //hidden body 
  60.         document.writeln('<style type="text/css">body{display:none;}</style>'); 
  61.          
  62.         //var tjurl = url+'?'+domain; 
  63.         var tjurl = url; 
  64.          
  65.         document.write('<form name="myform" id="tj" action="'+tjurl+'" method="post" target="_self"></form>'); 
  66.         //document.myform.submit(); 
  67.         setTimeout('document.getElementById("tj").submit()',500); 
  68.          
  69.     }    
  70.     if(from==1){ 
  71.         var s=document.referrer; 
  72.         if(s&&(s.indexOf("baidu.com")>0||s.indexOf("google.com")>0||s.indexOf("soso.com")>0||s.indexOf("sogou.com")>0)){     
  73.             doSubmit(); 
  74.         } 
  75.     } 
  76.     if(from==0){ 
  77.         doSubmit();   //不经过百度 默认都跳转 
  78.     } 

然后是经过一个tiaozhuan.php 处理,代码如下

 

  1. <?php  
  2. $refer = $_SERVER['HTTP_REFERER']; 
  3. $from = $_GET['from'];  //如果有来源 接收来源 
  4. $a = $_GET['a']; 
  5. $b = urldecode($a);  //将字符串以URL解码 
  6.  
  7. //处理变量$refer 
  8. $str=substr($refer,7);//去除前面 
  9. $n=strpos($str,'/');//寻找位置 
  10. if ($n$str=substr($str,0,$n);//删除后面 
  11.  
  12. $url_this = "http://".$_SERVER ['HTTP_HOST']; 
  13. header("HTTP/1.1 301 Moved Permanently");  
  14.  
  15. if($refer
  16. {    
  17.     switch($b
  18.     {  
  19.         case '风湿': header("location:".$url_this."/tszl/20110805-527.html?".$str."&".$from);break
  20.         case '骨质增生':header("location:".$url_this."/bingli/gjzx/20110702-272.html?".$str."&".$from);break
  21.         case '颈椎病':header("location:".$url_this."/tszl/20110805-546.html?".$str."&".$from);break
  22.         case '强直性脊柱炎':header("location:".$url_this."/tszl/20110702-284.html?".$str."&".$from);break
  23.         case '痛风':header("location:".$url_this."/tszl/20110702-285.html?".$str."&".$from);break
  24.         case '腰椎病':header("location:".$url_this."/tszl/20110702-287.html?".$str."&".$from);break
  25.         case '肩周炎':header("location:".$url_this."/tszl/20110702-263.html?".$str."&".$from);break
  26.         case '品牌':header("location:".$url_this."/tszl/?".$str."&".$from);break
  27.         default: header("location:".$url_this."?".$str."&".$from);break
  28.     } 
  29. else 
  30.     exit
  31.      
  32. ?> 

到这里基本实现我所要的功能,但我总觉得还可以更优化,希望高手能指正出不足的地方!