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:

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|)$?


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

RewriteEngine on
RewriteRule ^(.*)$ https://example.com/us/$1 [L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.