Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
How do you return the final redirect URL of a website using Google Apps Scripts

sorry if this is a simple question but I was wondering how I would be able to return the redirected URL of a webpage using google sheets.

Sample input webCode = SNSKS21KK

I've attached the code below:

function CHECKWEBSITE(webCode){ 
  if (webCode.map){
    return webCode.map(CHECKWEBSITE);
  } else {
    var url = 'https://www.torpedo7.co.nz/products/' + webCode;
    var redirectedUrl = ?;
    var options = {
      'muteHttpExceptions': true,
      'followRedirects': false
    };
    var response = UrlFetchApp.fetch(url, options);
    var status = response.getResponseCode();
    
    if (redirectedUrl.includes("discontinued") || status == 302) {
      return "Not Online"; 
    } else if (status == 200 || status == 301){ 
      return "Online";
    } else {
      return "Not imported";
    }
  }
} 

Expected redirect output = https://www.torpedo7.co.nz/products/SNSKS21KK/title/2022-e-s-force-fx-80-skis---m12-gw-f80-bindings

Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your situation, how about the following modification?

From:

var url = 'https://www.torpedo7.co.nz/products/' + webCode;
var redirectedUrl = ?;
var options = {
  'muteHttpExceptions': true,
  'followRedirects': false
};
var response = UrlFetchApp.fetch(url, options);
var status = response.getResponseCode();

To:

var url = 'https://www.torpedo7.co.nz/products/' + webCode;
var options = {
  'muteHttpExceptions': true,
  'followRedirects': false
};
var response = UrlFetchApp.fetch(url, options);
var redirectedUrl = response.getAllHeaders().Location;
var status = response.getResponseCode();
  • redirectedUrl is retrieved from Location of the response header.
  • When I tested the modified script using webCode = SNSKS21KK, redirectedUrl is https://www.torpedo7.co.nz/products/SNSKS21KK/title/2022-e-s-force-fx-80-skis---m12-gw-f80-bindings and Online was returned.

Reference:

  • fetch(url, params)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!