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>Set the redirect link (where you want it to go)
Put this script in the body of your page