Redirect Loop issue on browsers

Hi there

I’m having an issue whith redirect loop issue on browsers only when I try to redirect US to the full domain so for example domain.com. It works perfectly fine if I redirect it to another domain or use subfolder on main domain.

So I’m using the following htacces code

RewriteEngine on

RewriteOptions inherit

RewriteCond %{HTTP_HOST} domain/ com [NC]
RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ https:// domain/ com/$1 [R,L]

DirectoryIndex index.php

So my main traffic is from the US so I want them to stay on the domain only so not to a subfolder or any other domain, and for some countries I redirect them to a subfolder. To do this I’m using a php code. Note: I’ve tried also doing other coding via htaccess to do the following but I always had same redirect loop issue doing that also.

<?php $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; if ($country_code == 'US') { header("HTTP/1.1 301 Moved Permanently"); header("Location: https:// domain/com"); die(); } else if ($country_code == 'LU') { header("HTTP/1.1 301 Moved Permanently"); header("Location: https:// domain/com/LU/"); die(); } else if ($country_code == 'MA') { header("HTTP/1.1 301 Moved Permanently"); header("Location: https:// domain/com/MA/"); die(); } else header("HTTP/1.1 301 Moved Permanently"); header("Location: https:// google.com/"); die(); } ?>

What I want to achieve is that I only want the countries to get on my website and all other countries to google since I don’t want them to see my website at all. Issue I’m facing is that it won’t work if I redirect them back to the domain they are already on… so person 1 types in domain.com wich is from US and gets redirect error on his screen… while for the others it works perfectly fine since they are being redirected to the subfolder on that domain.

Can someone tell me what is causing this issue? (I’m using a vps centos/cpanel)

I had to put space and / in-between domain.com etc since I’m a new user

To avoid issues with posting excessive URLs, you can always mark them as code using the </> icon.

If the intention is never to allow visitors from outside the US, you could create a Firewall Rule to block visitors from any countries other than the US (and perhaps a few others you may want to whitelist):

image

The rule above would block any visits coming from outside the US, except for “known bots” such as Googlebot. But keep in mind that many good bots and online services may have infrastructure outside the US and not be on Cloudflare’s list. Facebook and Twitter are two that comes to mind.

This topic was automatically closed after 30 days. New replies are no longer allowed.