使用 javascript 或 php 将所有子域的 Https 更改为 link 中的 Http

Change Https to Http in a link for all subdomain using javascript or php

我需要一个脚本(JavaScript 或 PHP 而不是 mod_rewrite),它可以将所有 *subdomain.example.com link 上的所有 HTTPS 替换为 HTTP 但保留example.com 和 www.example.com.

的 HTTPS

例:我要转

<a href="HTTPS://sub.example.com">My sub domain</a>

<a href="HTTP://sub.example.com">My sub domain</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>

PHP 中有一个解决方案。它还涵盖 subsub.sub.example.com 等子域和 example.com.

等域名

请注意,当您想在服务器端执行此操作时……

  • 脚本执行 redirect(没有其他选择)将协议从 https 更改为 http.
  • (php) 脚本可以获得控制之前建立了一个加密连接。
  • 因此,对于(例如)证书与某些子域名不匹配而其他子域名匹配的情况,您不能使用服务器端解决方案。

    <?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'))
}

通过 https://www.google.com/search?q=google 的控制台测试(在 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://'))
}