Redirecting facebook referral to the specific url

Hi @shagors333,

Try this updated code:

exports.handler = async event => {
  if (event.queryStringParameters.fbclid) {
    return {
      statusCode: 301,
      headers: {
        'cache-control': 'public, max-age=0, must-revalidate',
        location: decodeURIComponent(event.queryStringParameters.url)
      }
    }
  } else {
    return {
      statusCode: 301,
      headers: {
        'cache-control': 'public, max-age=0, must-revalidate',
        location: process.env.URL + '/' + decodeURIComponent(event.queryStringParameters.url).split('/')[3] + '/'
      }
    }
  }
}

How it works: It checks for the fbclid query string parameter that Facebook attaches on any external link opened from the platform. HOWEVER, do note that, this might not work well with ad blockers. I am using AdGuard and spent quite some time thinking why it was not working, until I remembered that the fbclid parameter was getting removed by it. In such a case, the function will process the URL as if it didn’t come from Facebook.

Now, when the URL comes from Facebook, the user will be redirected to the page on your custom domain (the URL that you enter as an encoded string). However, if the URL doesn’t come from Facebook, it will automatically redirect you to the primary domain of the website along with the path from the url query parameter. This has just been tested, so it should work.