使用javascript或php将所有子域的链接中的Https更改为Http
我需要一个脚本(JavaScript或PHP而不是mod_rewrite),它可以替换所有*subdomain.example.com链接上的所有HTTPS到HTTP,但保留example.com和www.example.com的HTTPS 我想转身使用javascript或php将所有子域的链接中的Https更改为Http,javascript,php,http,https,subdomain,Javascript,Php,Http,Https,Subdomain,我需要一个脚本(JavaScript或PHP而不是mod_rewrite),它可以替换所有*subdomain.example.com链接上的所有HTTPS到HTTP,但保留example.com和www.example.com的HTTPS 我想转身 到 *将有许多不同的子域… <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <a
到
*将有许多不同的子域…
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<a href="https://www.test.com" >Test 1 </a><br>
<a href="https://ftp.test.com" >Test 2 </a><br>
<a href="https://test.test.com" >Test 3 </a><br>
<script type="text/javascript">
function extractDomain(url) {
var domain;
//find & remove protocol (http, ftp, etc.) and get domain
if (url.indexOf("://") > -1) {
domain = url.split('/')[2];
}
else {
domain = url.split('/')[0];
}
//find & remove port number
domain = domain.split(':')[0];
domain = domain.replace("www.", "");
return domain;
}
$("a").each(function(){
var url = $(this).attr("href");
var res = extractDomain( url );
var resSplit = res.split('.');
if( resSplit.length > 2 ){
$(this).attr("href", url.replace("https","http") );
}
});
</script>
函数提取域(url){
var域;
//查找并删除协议(http、ftp等)并获取域
如果(url.indexOf(“:/”>)为-1){
domain=url.split('/')[2];
}
否则{
domain=url.split('/')[0];
}
//查找并删除端口号
domain=domain.split(“:”)[0];
domain=domain.replace(“www.,”);
返回域;
}
$(“a”)。每个(函数(){
var url=$(this.attr(“href”);
var res=提取域(url);
var resSplit=res.split('.');
如果(resSplit.length>2){
$(this.attr(“href”,url.replace(“https”,“http”);
}
});
这里有一个PHP解决方案。它还包括subsub.sub.example.com等子域和example.com等域名
请注意,当您想在服务器端执行此操作时
- 该脚本执行重定向(没有替代方案)将协议从https更改为http
- 在(php)脚本获得控制权之前,已建立与SSL/HTTPS的加密连接
- 因此,如果(例如)证书与某些子域名称不匹配,而其他子域名称匹配,则不能使用服务器端解决方案
<?php $tochange = array( 'sub', 'sub1', 'test', ); $hname = getenv("SERVER_NAME"); if (preg_match('/^(.*)(\.(.*?)\.(.*))$/s', $hname, $regs)) { // $regs[1] contains subdomain name / names (also: *subsub.sub.domain.tld"). $encrypted = ((isset($_SERVER["HTTPS"])) && (strtoupper($_SERVER["HTTPS"]) == 'ON')); if ($encrypted && (in_array($regs[1],$tochange))) { $port = getenv("SERVER_PORT"); $query = ($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : ''; $url = 'httpx://' . $hname . $_SERVER['SCRIPT_NAME'] . $query; // Do a redirect header("location: $url"); // redirect // echo "$url<br >"; // for testing } } ?>
不需要jQuery的解决方案:var as = document.getElementsByTagName('a') //get all a tags var re = /^https:\/\/[\w\W]*(example.com)/i //http://*example.com var reExcept = /^https:\/\/(www.)?(example.com)/i //filter https://www.example.com and http://example.com for (var i=0; i<as.length; i++) { href = as[i].getAttribute('href') console.log('original href: ' + href) if (!href || !re.test(href) || reExcept.test(href) ) continue //this href shouldn't be replaced href = href.replace('https://', 'http://') as[i].setAttribute('href', href) console.log('replaced href: ' + as[i].getAttribute('href')) }
var as=document.getElementsByTagName('a')//获取所有a标记 var re=/^https:\/\/[\w\w]*(example.com)/i//http://*example.com var reExcept=/^https:\/\/(www.)?(example.com)/i//filterhttps://www.example.com 及http://example.com for(var i=0;i)(在re和reExept中使用google.com而不是example.com)。似乎工作正常 少一点罗嗦的版本:
var re = /^https:\/\/[\w\W]*(example.com)/i var reExcept = /^https:\/\/(www.)?(example.com)/i var as = document.getElementsByTagName('a') for (var i=0; i<as.length; i++) { href = as[i].getAttribute('href') if ( !href || !re.test(href) || reExcept.test(href) ) continue as[i].setAttribute('href', href.replace(/^https:\/\//i, 'http://')) }
var re=/^https:\/\/[\w\w]*(example.com)/i var reExcept=/^https:\/\/(www.)?(example.com)/i var as=document.getElementsByTagName('a')
对于(var i=0;i如果子域小于2,该怎么办?@dvenkatsagar子域如何小于2。示例?@Owais Shisha如果可行,请将其标记为正确答案,智能手机有什么问题?代码中没有什么特殊之处,只有标准javascript。分别测试代码中使用的每个函数,可能只有一个iphone浏览器不支持这些功能(我对iphone一无所知)。