【问题标题】:What Google AppsScript method is used to get the URL of a redirect?使用什么 Google Apps 脚本方法来获取重定向的 URL?
【发布时间】:2014-11-24 05:03:11
【问题描述】:

“www.mysite.com/mySecretKey1”重定向到“www.othersite.com/mySecretKey2”

在 G.AppsScript 中:

  var response = UrlFetchApp.fetch("https://www.mysite.com/mySecretKey1");
  var headerString = response.getAllHeaders().toSource();
  Logger.log(headerString);
  //string 'www.othersite.com.my/SecretKey2' is not present in log.

脚本如何发现它被重定向到的 URL 地址(即字符串“www.othersite.com/mySecretKey2”)?

更新:更一般地说,脚本如何从response 中发现 URL 地址?

【问题讨论】:

    标签: google-apps-script redirect urlfetch


    【解决方案1】:

    answer by Joseph Combs 上进行说明,这里有一个版本,它使用递归来跟踪多个重定向,只返回最终的规范 URL:

    function getRedirect(url) {
      var response = UrlFetchApp.fetch(url, {'followRedirects': false, 'muteHttpExceptions': false});
      var redirectUrl = response.getHeaders()['Location']; // undefined if no redirect, so...
      var responseCode = response.getResponseCode();
      if (redirectUrl) {                                   // ...if redirected...
        var nextRedirectUrl = getRedirect(redirectUrl);    // ...it calls itself recursively...
        Logger.log(url + " is redirecting to " + redirectUrl + ". (" + responseCode + ")");
        return nextRedirectUrl;
      }
      else {                                               // ...until it's not
        Logger.log(url + " is canonical. (" + responseCode + ")");
        return url;
      }
    }  
    
    function testGetRedirect() {
      Logger.log("Returned: " + getRedirect("http://wikipedia.org"));
    }
    

    此日志:

    https://www.wikipedia.org/ is canonical. (200)
    https://wikipedia.org/ is redirecting to https://www.wikipedia.org/. (301)
    http://wikipedia.org is redirecting to https://wikipedia.org/. (301)
    Returned: https://www.wikipedia.org/
    

    【讨论】:

      【解决方案2】:

      更新:更一般地说,脚本如何从响应中发现 URL 地址?

      与直觉相反,您需要禁用重定向并且静音 HttpExceptions,如下所示:

      var followedPost = UrlFetchApp.fetch(properUrl, {'followRedirects': false, 'muteHttpExceptions': false});
      Logger.log(followedPost.getHeaders()['Location']);
      

      .getHeaders() 返回的对象将包含被请求资源的新位置。使用新的 .fetch() 访问该新位置。

      【讨论】:

        【解决方案3】:

        在 UrlFetchApp 中有一个原生支持来跟踪重定向。 你应该尝试设置:

        followRedirects = true
        

        在您提供给 UrlFetchApp 的选项中。 类似的东西:

        var options = {
           "followRedirects" : true
         };
        var result = UrlFetchApp.getRequest("http://your-url", options);
        

        【讨论】:

        • response 中仍然没有第一页或第二页的 URL 地址。
        【解决方案4】:

        这里有一个 google sheet 可以免费复制 https://www.thetechseo.com/seo-tools/redirect-checker/

        它完美地提供了跳转、代码和目的地。

        以防万一(工作表/页面丢失)我在此处粘贴脚本编辑器中的代码(这些代码都不是我的)。

        function redirectCheck(url, user, pwd) {
          try {
            function getResp(url, user, pwd){  
              var resp = UrlFetchApp.fetch(url, {
                muteHttpExceptions: true,
                followRedirects: false,
                headers: {
                  'Authorization': 'Basic ' + Utilities.base64Encode(user+':'+pwd)
                }
              });
              return resp;
            }
        
        
        var response = getResp(url, user, pwd);
        var rCode = response.getResponseCode();
        var redirectCount = 0;
        var tCode = rCode.toString();
        var location = url;
        var domain = getDomain(url);
        
        while (rCode == 301 || rCode == 302 && redirectCount <= 10) {
          redirectCount++;
          header = response.getHeaders();
          location = getFullUrl(header['Location'],domain);
          domain = getDomain(location);
          Logger.log('location: '+location);
          response = getResp(location, user, pwd);
          rCode = response.getResponseCode(); 
          tCode = tCode + " > " + rCode.toString();
          Utilities.sleep(500);// pause in the loop for 500 milliseconds
        }     
        
        
        Logger.log('redirectCount: '+redirectCount);
        return tCode + "|" + redirectCount + "|" + location;
        
        
          } catch (error) {
            Logger.log(error);
            return "Error| |"+error;
          }
        }
        function getDomain(url) {
          var domain = '',
              protocol;
          if (url.indexOf("://") > -1) {
            domain = url.split('/')[2];
            protocol = url.split('/')[0];    
            //remove port number
            domain = domain.split(':')[0];
            //add protocol back
            domain = protocol+"//"+domain;
          }  
        
          return domain;
        }
        
        function getFullUrl(url,prevDom) {
          var fullUrl,
              domain = getDomain(url);
          if(domain == ''){
            fullUrl = prevDom+url;
          } else {
            fullUrl = url;
          }       
        
          return fullUrl;
        }
        
        function redirectCheckTest() {
          var test = redirectCheck('http://blog.pexcard.com/contractors/building-budget-construction-business/');
          Logger.log('test: '+test);
        }
        

        还有公式

        在G中

        =IF(H11=200,"Not Redirected",IF(ISBLANK(C11),"",if(C11=J11,"Good","Bad")))
        

        在 H

        =IF(ISBLANK(B11),"",split(redirectCheck(B11,$L$5,$L$6),"|"))
        

        它不仅可以让您发现问题(错误代码),还可以通过将链接替换为最终目的地来提高链接质量。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-15
          • 2014-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多