首页 站长正文

简单实用的javascript重定向,js将旧域名网址目录跳转到新网址

康乔 站长 2019-10-07 58 0

JS重定向代码

<script language="javascript">
if (document.domain =="kangqiao.org")     
this.location = "http://www.kangqiao.org" + this.location.pathname + this.location.search;     
</script>

多个域名跳转到指定域名

<script>
if (document.domain =="a.kangqiao.org" || document.domain =="b.kangqiao.org"  || document.domain =="c.kangqiao.org")
this.location = "http://www.kangqiao.org" + this.location.pathname + this.location.search;
</script>

(会将多个域名的http和https跳转到http://www.kangqiao.org)

优化版:多个域名跳转到指定域名

<script>
if (document.domain !="kangqiao.org")
this.location = "http://www.kangqiao.org" + this.location.pathname + this.location.search;     
</script>

(访问当前页面只要域名不是kangqiao.org都会自动跳转到http://www.kangqiao.org)

http强制js跳转到https

<script type="text/javascript">
    var targetProtocol = "https:";
    if (window.location.protocol != targetProtocol)
        window.location.href = targetProtocol +
        window.location.href.substring(window.location.protocol.length);
</script>

评论