Logo

What are you looking for?

Get help straight from our team...

Block / Redirect By Country Script

Other

Block / Redirect By Country Script

Want to block certain countries from your website, landing page, webinar registrations? Use this.

Last updated on 10 Jun, 2026

JavaScript<script>
(function () {
  // ========================= CONFIGURE ME =========================
  var REDIRECT_TO = "https://example.com/welcome";   // send them here

  var COUNTRIES = ["IN", "PK", "BD"];                // forward these countries
  // ================================================================

  // Don't redirect if we're already on the destination page (stops loops)
  if (window.location.href.indexOf(REDIRECT_TO) === 0) return;

  // Forward the visitor if their country is in the list
  function maybeRedirect(country) {
    if (!country) return;
    if (COUNTRIES.indexOf(country.toUpperCase()) !== -1) {
      window.location.replace(REDIRECT_TO);
    }
  }

  // Primary lookup (ipapi.co). If it fails, fall back to country.is.
  fetch("https://ipapi.co/json/")
    .then(function (r) { return r.json(); })
    .then(function (d) { maybeRedirect(d && (d.country_code || d.country)); })
    .catch(function () {
      fetch("https://api.country.is/")
        .then(function (r) { return r.json(); })
        .then(function (d) { maybeRedirect(d && d.country); })
        .catch(function () { /* both failed: leave the visitor where they are */ });
    });
})();
</script>
  1. Set the redirect link (where you want it to go)

  2. Put this script in the body of your page

Did you find this article helpful?