利用404错误页面实现UrlRewrite的实现代码

  将下面代码保存到err404.html,然后设置404错误页面为err404.html页;

  

复制代码 代码如下:

  <script type="text/javascript" language="javascript">

  var id=/(detial|show)\.asp\?id\=([0-9]+)/gi;

  var flag=/([a-z0-9_]+)\.asp\?flag\=xml/gi;

  var re_id=/(detial|show)\_([0-9]+)\.html/gi;

  var re_flag=/([a-z0-9_]+)\.rss/gi;

  var host='blog.ii-home.cn';

  var ss=window.location.href;

  //ss=ss.replace(window.location.hostname,host);

  var Temp=getasp(ss);

  getNotice(Temp);

  function createobj() {

  if (window.ActiveXObject) {

  return new ActiveXObject("Microsoft.XMLHTTP");

  }

  else if (window.XMLHttpRequest) {

  return new XMLHttpRequest();

  }

  }

  function getNotice(url) {

  var oBao=createobj();

  var my_url=url;

  oBao.open('get',my_url,false);

  oBao.onreadystatechange=function(){

  if(oBao.readyState==4){

  if(oBao.status==200){

  var returnStr=oBao.responseText;

  var Temps=gethtml(returnStr);

  document.write(Temps);

  }else{

  document.write("未找到您输入的地址或服务器505错误!");

  }

  }

  }

  oBao.send(null);

  }

  //将str中的asp网址按照正则替换

  function gethtml(str){

  var Temp_str=str.replace(id,'$1_$2.html');

  Temp_str=Temp_str.replace(/index\.asp/gi,'index.html');

  Temp_str=Temp_str.replace(/myfiles\.asp/gi,'myfiles.html');

  Temp_str=Temp_str.replace(/mydiary\.asp\?typ\=self/gi,'mydiary_self.html');

  Temp_str=Temp_str.replace(/mydiary\.asp/gi,'mydiary.html');

  Temp_str=Temp_str.replace(flag,'$1.rss');

  Temp_str=Temp_str.replace(/xml_diary\.asp/gi,'xml_diary.html');

  Temp_str=Temp_str.replace(/xml_download\.asp/gi,'xml_download.html');

  Temp_str=Temp_str.replace(/xml_links\.asp/gi,'xml_links.html');

  Temp_str=Temp_str.replace(/xml_ly\.asp/gi,'xml_ly.html');

  Temp_str=Temp_str.replace(/ly\.asp/gi,'ly.html');

  //Temp_str=Temp_str.replace(/href=\"(http){0}/gi,'href="http://blog.ii-home.cn/');

  return(Temp_str);

  }

  //将URL替换成原地址

  function getasp(str){

  var Temp_str=str.replace(re_id,'$1.asp?id=$2');

  Temp_str=Temp_str.replace(/index\.html/gi,'index.asp');

  Temp_str=Temp_str.replace(/myfiles\.html/gi,'myfiles.asp');

  Temp_str=Temp_str.replace(/mydiary\.html/gi,'mydiary.asp');

  Temp_str=Temp_str.replace(/mydiary\_self.html/gi,'mydiary.asp?typ=self');

  Temp_str=Temp_str.replace(/xml_diary\.html/gi,'xml_diary.asp');

  Temp_str=Temp_str.replace(/xml_download\.html/gi,'xml_download.asp');

  Temp_str=Temp_str.replace(/xml_links\.html/gi,'xml_links.asp');

  Temp_str=Temp_str.replace(/xml_ly\.html/gi,'xml_ly.asp');

  Temp_str=Temp_str.replace(/ly\.html/gi,'ly.asp');

  Temp_str=Temp_str.replace(re_flag,'$1.asp?flag=xml');

  return(Temp_str);

  }

  </script>