【问题标题】:Change Https to Http in a link for all subdomain using javascript or php使用 javascript 或 php 在所有子域的链接中将 Https 更改为 Http
【发布时间】:2016-05-08 01:28:39
【问题描述】:

我需要一个脚本(JavaScript 或 PHP,而不是 mod_rewrite),它可以将所有 *subdomain.example.com 链接上的所有 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>

*会有很多不同的子域...

【问题讨论】:

    标签: javascript php http https subdomain


    【解决方案1】:
    <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>
    

    【讨论】:

    • 子域小于2怎么办?那么它会工作吗??
    • @dvenkatsagar 子域怎么可能小于 2。示例?
    • @Owais Shisha 如果有效,请将其标记为正确答案
    【解决方案2】:

    这是 PHP 中的一个解决方案。它还涵盖像 subsub.sub.example.com 这样的子域和仅像 example.com 这样的域名。

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

    • 脚本执行重定向(没有替代方案)以将协议从 https 更改为 http。
    • 与 SSL/HTTPS 的加密连接在 (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
          }
      }
      ?>
      

    【讨论】:

      【解决方案3】:

      不需要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://'))
      }
      

      【讨论】:

      • 智能手机出了什么问题?代码中没有什么特别之处,只是标准的 javascript。
      • 分别测试代码中使用的每个功能,可能其中一个功能不受iphone浏览器支持(我对iphone一无所知)。
      猜你喜欢
      • 2014-10-26
      • 2016-11-22
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多