0

I want to add country base redirection and add code in the .htaccess file, for example: If the user open site in UK then open this domain xyz.co.uk and other redirect to this domain xyz.com

here is my .htaccess code:

RewriteEngine On
RewriteBase /

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(UK)$
RewriteRule ^(.*)$ xyz.co.uk/$1 [L]

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^(UK)$
RewriteRule ^(.*)$ xyz.com/$1 [L]


RewriteRule ^(.*)?$ 127.0.0.1:3000/$1 [P,L]

Result: redirection working but given error too many redirects

MrWhite
  • 43,179
  • 8
  • 60
  • 84
navedhn
  • 3
  • 4
  • So both `xyz.co.uk` and `xyz.com` point to the same webspace directory? Well then of course this will get you an endless redirect loop, because you are redirecting the visitor from the UK, who just requested `xyz.co.uk`, again ... You'll need to add another RewriteCond to each case, that checks if the requested host name was not already the one you would be redirecting to. – CBroe Aug 04 '23 at 13:43
  • @CBroe can you please elaborate with code. Thank you! – navedhn Aug 04 '23 at 13:56
  • https://stackoverflow.com/q/10232722/1427878 (You can negate a condition by putting a `!`in front of it.) – CBroe Aug 04 '23 at 14:06
  • Although there are no "redirects" in the code you've posted. These are internal rewrites to a `/xyz.co.uk` (and `/xyz.com`) "subdirectory". (Maybe you've "broken" these rules in an attempt to exemplify it?) – MrWhite Aug 04 '23 at 14:28
  • @MrWhite can you please give me an example so I can better understand Thank you! – navedhn Aug 05 '23 at 11:40

0 Answers0