Question

I wrote this in my htaccess:

GeoIPEnable On 
RewriteEngine On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} PE
RewriteCond %{HTTP_HOST} myweb.com [NC]
RewriteCond %{HTTP_HOST} !es [NC]
RewriteCond %{REQUEST_URI} !^/es/ [NC]
RewriteRule ^(.*)$ /es/$1 [R,L] 

SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css application/x-javascript
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

I want users from Peru and another countries to be redirected to http://myweb.com/es

But I got an error: http://myweb.com/es/es/es/es/es/es/es/...s/es/es/es/es/ How can I fix it? thank you.

Was it helpful?

Solution

Use a negative lookahead regex to avoid redirecting when it is already /es/:

GeoIPEnable On 
RewriteEngine On

RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteCond %{REQUEST_URI} !^/(es/|index\.php) [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} PE
RewriteCond %{HTTP_HOST} myweb\.com$ [NC]
RewriteCond %{HTTP_HOST} !es [NC]
RewriteRule ^(.*)$ /es/$1 [R,L] 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top