Skip to content

Instantly share code, notes, and snippets.

@tomdavidson
Created December 31, 2015 06:17
Show Gist options
  • Save tomdavidson/0136ea6ce14e2d0a28fb to your computer and use it in GitHub Desktop.
Save tomdavidson/0136ea6ce14e2d0a28fb to your computer and use it in GitHub Desktop.
redirects utah ip addresses to the utah home page
<script>
(function() {
var geoIpServiceUrl = 'https://freegeoip.net/json/';
var redirectUrl = 'http://youturn.org/ut';
var isBotSpider = Boolean(navigator.userAgent.match(/bot|spider/i));
var alreadyUtRedirected = Boolean(document.cookie.replace(/(?:(?:^|.*;\s*)utRedirected\s*\=\s*([^;]*).*$)|^.*$/, '$1'));
var atRoot = window.location.pathname === '/';
var shouldRedirect = atRoot && !alreadyUtRedirected && !isBotSpider;
function redirect(response) {
if (response.region_name == 'Utah') {
document.cookie = 'utRedirected=true';
window.location.replace(redirectUrl);
}
}
if (shouldRedirect) {
jQuery.get(geoIpServiceUrl, redirect, "jsonp");
}
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment