当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 利用404错误页面实现UrlRewrite的实现代码

Javascript
在js中使用"with"语句中跨frame的变量引用问题
原型方法的不同写法居然会影响调试的解决方法
在JavaScript中遭遇级联表达式陷阱
JScript内置对象Array中元素的删除方法
获取JavaScript用户自定义类的类名称的代码
使用TextRange获取输入框中光标的位置的代码
使用onbeforeunload属性后的副作用
IE7提供XMLHttpRequest对象为兼容
encode脚本和normal脚本混用的问题与解决方法
用js判断用户浏览器是否是XP SP2的IE6
关于使用runtimeStyle属性问题讨论文章
javascript学习随笔(使用window和frame)的技巧
javascript学习随笔(编写浏览器脚本 Navigator Scripting )
跑马灯效果大全
让超链接显示提示信息的js代码
打开超链需要“确认”对话框的方法
javascript的对话框详解与参数
让网页上的超链接失效,不能点击的js代码
鼠标经过时链接文字的特别震撼的显示效果
显示页面的所有链接的js代码

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 324 ::
收藏到网摘: n/a

要求:网站编码为utf-8,不适用于GB2312; 替换字符的正则可以自己增加和修改,以适合自己的网站; 将下面代码保存到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>
"