1

I am using Apache's GeoIP module and a MaxMind database to determine the country of a visitor based on their IP address and redirecting them to a country sub-folder as shown below:

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^US$
RewriteRule ^(.*)$ https://example.com/us$1 [L]

I would like to redirect all countries to the /us sub-folder except China and Russia. A list of country codes are listed here, but I'll prefer to apply a wildcard to the rewrite condition as opposed to listing every country in my .htaccess file.

Is it possible to do something like RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^*$? Or do I have to expand the rewrite condition with all countries like RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(US|CA|ZA|NG|GH|)$?

Ralph
  • 802
  • 11
  • 25

1 Answers1

1

You could simply use negation ! to redirect anything but China CN and Russia RU.

RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^(CN|RU)$
RewriteRule ^(.*)$ https://example.com/us/$1 [L]
Esa Jokinen
  • 43,252
  • 2
  • 75
  • 122