网站电脑端与手机端js跳转设置?

由于iis只能设置一个独立域名ssl证书(多域名、泛域名贵),所以我电脑端用的https,手机端用的http。
在用户访问时:
function IsPC() {
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}
if(!IsPC()&&window.location.hostname=='www.rengsou.com'){
window.location.href = window.location.href.replace('www.rengsou.com','m.rengsou.com');
}
用户直接输入https电脑端时,跳转手机端也是加https,提示证书错误不跳转,所以我再前面加了:
var targetProtocol = "https:";
if (window.location.protocol != targetProtocol)
 window.location.href = targetProtocol +
 window.location.href = targetProtocol +
  window.location.href.substring(window.location.protocol.length);
电脑端输入正常,手机端输入http电脑端网址出现一直循环跳转
我应该怎么写呢,才能实现电脑端输入http和https都正常手机端输入http和https电脑端网址都正常调转手机端网址
 

其实用nginx代理的话,会把你自己去写这种拼接 更省事

如果是动态网页可以存cookie标识,后端判断cookie类型返回不同的界面

直接在后端添加http自动跳转至https不就可以了,用nginx配置证书执行转发