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

Javascript
一个短小精悍使用的对象化QQ菜单
用数据岛生成翻页程序
轻松实现删除确认
IE5中用JavaScript跨frame加option问题
IE6无提示关闭窗口,不是利用activeX
下拉框联动
用dhtml做了一个密码管理器
面向对象的JavaScript编程
网 络 病 毒 与 防 范 措 施
破解网页禁止鼠标右键的技巧
JS编写的俄罗斯方块
通过代码改变客户端所显示的语言类型
欢迎精灵
事件处理函数OnEnter OnExit 使用一例
称三次从12球中找出唯一但不知轻重的球
VML实现的饼图(JavaScript类封装)
搜索gb2312汉字在网上的频率
真正的 用JS 做的 loading
Vml:应用阿基米德算法在网页制作动画,原程+注释
贴一例:当所有图片下载完毕时,然后显示网页(有进度)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 90 ::
收藏到网摘: 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>
"