Return link to redirect url

I'm trying to get the url of the final destination of a specific website, but all the templates I've found to use as a function in my spreadsheet, only return the initial link:

https://stackoverflow.com/a/50733029

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;
  }
} 

This is the model where I put:
=getRedirect("https://c.newsnow.co.uk/A/1067471289?-833:12")

In the spreadsheet it returns:
https://c.newsnow.co.uk/A/1067471289?-833:12

I would like to collect the link to after redirect:
https://sports.ndtv.com/football/europa-league-bruno-fernandes-double-helps-manchester-united-thrash-real-sociedad-gareth-bale-stars-for-tottenham-2373767



Read more here: https://stackoverflow.com/questions/66271687/return-link-to-redirect-url

Content Attribution

This content was originally published by Brondby IF at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: