Jamers Posted January 10, 2015 Report Share Posted January 10, 2015 由于备案方面的原因,主域名,例如zomew.net必须指向备案IP地址,然后我们一般由于各种原因需要将网站放在第三方服务器或者使用CDN进行加速处理。如果直接指向备案IP就达不到加速效果。想办法处理一下吧。仅仅是javascript的一个小小的应用。 将此代码保存成一个html页面,然后将所有访问备案IP地址的请求均跳转到此页面上(可以是首页和404页面),服务器会根据访问内容自动跳转至对应页面上。目前这里针对的是主域名例如 zomew.net,对于含二级域名的网址,这里未匹配。举例: 当访问 http://zomew.net:80/test/test1.html跳转到下列内容的时候,将会自动跳转到 http://www.zomew.net:80/test/test1.html <script language="javascript"> <!-- ///^(http[s]?):\/\/([\w\d]++\.\w++(?:[:]+[\d]++)?([\/]+.*)?)$/i var p = /^(http[s]?):\/\/([\w\d]{1,}\.\w{1,}?(?:[:]+[\d]{1,})?([\/]+.*)?)$/i; var out = ''; url = window.location.href; //document.write(url); if(p.test(url)) { out = RegExp.$1+'://www.'+RegExp.$2; window.location.href = out; //alert(out); }; --> </script> Link to comment Share on other sites More sharing options...
Jamers Posted January 10, 2015 Author Report Share Posted January 10, 2015 javascript正则中不能够使用 ++ 匹配符,否则会显示 SyntaxError: nothing to repeat 错误。 Link to comment Share on other sites More sharing options...
Jamers Posted February 25, 2016 Author Report Share Posted February 25, 2016 <script> var href = window.location.href; var p = /^(http[s]?):\/\/([\w\d]+)\.([^\/]*)(?:[:]{1}[\d]+)?[\/]{1}([\d\w\.]+)$/im; if (p.test(href)) { out = RegExp.$1+'://www.'+RegExp.$3; window.location.href = out; } </script> 一个简单的应用 。 Link to comment Share on other sites More sharing options...
上海龙天阳 Posted February 25, 2016 Report Share Posted February 25, 2016 <script> var href = window.location.href; var p = /^(http[s]?):\/\/([\w\d]+)\.([^\/]*)(?:[:]{1}[\d]+)?[\/]{1}([\d\w\.]+)$/im; if (p.test(href)) { out = RegExp.$1+'://www.'+RegExp.$3; window.location.href = out; } </script> html和php的网址域名上收尾巴的代码如上。 Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now