使用JS的简单方法实现Rewrite的301域名转向

在做网站项目的时候,由于新老域名的问题,需要做301跳转,但发现虚拟主机不支持伪静态(Rewrite)功能,而项目全是php生成html的,动态方法也无法实现。

于是想到了JS,不管是动态和静态页面都可以完美支持。分享出来,下面是JS代码实现方法:

<script type="text/javascript"&gt;

/* 跳转JS代码 */

try

{

var s_domain = location.host;

var js_path = "www.68080.com";

if (s_domain != js_path)

{

var str=window.location.href;

var es=/sh96.cn/;

es.exec(str);

var right=RegExp.rightContext;

top.location.href = "http://"+js_path+right;

}

}

catch(e) { }

</script>

代码解释:

代码中的第6行 www.68080.com 是新的域名地址,第10行 sh96.cn 则是老的域名。

代码例子:

当用户访问 http://sh96.cn/a/yinyue.html 的时候,

自动跳转到 http://www.68080.com/a/yinyue.html 

评论(0)