Check HTTP Status Code and Redirected URLs on Google Sheets

Check HTTP Status Code and Redirected URLs on Google Sheets

Checking redirects and status codes can be very complicated when you’re using a third-party website.

But this can be automated using Google sheets and if you don’t have a lot of knowledge in coding then you don’t need to worry.

You will also be able to know the chains of redirect with this code.

check HTTP Status Codes and Redirected URLs on Google Sheets

Script in Action

check HTTP Status Codes and Redirected URLs on Google Sheets

How to Check HTTP status codes and Redirected URLs on Google Sheets

  • Firstly, Open the Google Sheets from here – Create a New Google Sheets
  • Then you need to goto Tools –> Script Editor
  • You can copy and paste the below given code.
function getStatusCode(url) {
    const url_trimmed = url.trim();

    let cache = CacheService.getScriptCache();
    let result = cache.get(url_trimmed);


    if (!result) {

        const options = {
            'muteHttpExceptions': true,
            'followRedirects': false
        };
        const response = UrlFetchApp.fetch(url_trimmed, options);
        const responseCode = response.getResponseCode();


        cache.put(url_trimmed, responseCode, 21600);
        result = responseCode;
    }

    return result;
}

function getRedirects(url) {
    
    const urlKey = url.trim()+"k";
    let cache = CacheService.getScriptCache();
    let result = cache.get(urlKey);


    if (!result) {
        const params = {
            'followRedirects': false,
            'muteHttpExceptions': true
        };
        const res = UrlFetchApp.fetch(url, params);
        const finalURL = res.getHeaders()['Location'];

        cache.put(urlKey, finalURL, 21600);
        
        result = finalURL;
    }

    return result;

}
  • Then you need to click on the run button
check HTTP Status Codes and Redirected URLs on Google Sheets
  • It will ask you for a few permission. You can allow those permissions.
  • Then you can add these formula’s
=getStatusCode(A1)
=getRedirects(A1)

A1 represents any cell, and you can drag the formula down to check status codes and redirects for multiple URLs.

To make it quicker, there’s also a cache built into the script, so that the formula won’t refresh with new values for up to 6 hours.

check HTTP Status Codes and Redirected URLs on Google Sheets

Final Verdict

So, this is how you can easily check HTTP Status Codes and Redirected URLs on Google Sheets. I hope you loved it.

The inspiration for this app script is taken from keywords in the sheet website.

If this script isn’t working or if there is an issue let us know in the comments section. We will be replying to the comments in 8 hours.

Share on:

I am the founder of WebVerge. I have 5+ years of experience in WordPress. I always write about the things I am obsessed with tech, WordPress, and SEO. I am also a Digital Marketing trainer at IIDE.

3 thoughts on “Check HTTP Status Code and Redirected URLs on Google Sheets”

  1. Hi, I am getting this error

    TypeError: Cannot read property ‘url_trimm’ of undefined
    getStatusCode @ Code.gs:2

    How to resolve this?

    Reply

Leave a Comment