Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
litespeed_wiki:cache:common:geoip-support [2019/01/08 19:30] Lisa Clarke [''GeoIPDBFile'' directive is for Apache, not LSWS] |
litespeed_wiki:cache:common:geoip-support [2019/01/08 19:35] Lisa Clarke [Why infinite redirect for my GeoIP rewrite Rules] |
||
---|---|---|---|
Line 123: | Line 123: | ||
In Apache, you can use ''GeoIPDBFile'' directive to define the database, however it cannot be used for LiteSpeed. You should follow the beginning steps in this wiki to define the database path from LSWS Web Admin Console or the LSWS configuration file directly. | In Apache, you can use ''GeoIPDBFile'' directive to define the database, however it cannot be used for LiteSpeed. You should follow the beginning steps in this wiki to define the database path from LSWS Web Admin Console or the LSWS configuration file directly. | ||
- | ==== Why infinite redirect for my GeoIP rewrite Rules ==== | + | ==== GeoIP Rewrite Rules Infinite Loop ==== |
- | A user would like to setup GeoIP rules to direct traffic to main domain's subfolder based on IPs. The following rules have been set in .htaccess, however, it seems to come to redirect loop hence claimed GeoIP was not working properly. | + | A user would like to set up GeoIP rules to direct traffic to the main domain's subfolder based on IP. The following rules have been set in .htaccess, however, it seems to cause a redirect loop. |
RewriteEngine on | RewriteEngine on | ||
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^US$ | RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^US$ | ||
Line 134: | Line 134: | ||
RewriteRule ^(.*)$ https://www.example.com/my/$1 [R,L] | RewriteRule ^(.*)$ https://www.example.com/my/$1 [R,L] | ||
- | If it came to a redirect loop, it means GeoIP module was actually working. How the redirect loop occurs? While for such subfolder redirect, you might need to be more careful. The reason you get a redirect error, is because the rules will be evaluated again after the redirect has been performed, so if you access ''/'' it gets redirected to e.g. ''/us'', then on ''/us'', it will be asked to redirect again to ''/us'' - and you end up with a loop. The fix is to add an additional condition to prevent this, such as ''RewriteCond %{REQUEST_URI} !^/us[NA]''. So what it does, is to only redirect to /us if country code from the GeoIP matches US and the request URI doesn't start with /us. | + | The redirect loop actually indicates the the GeoIP module is working. |
+ | |||
+ | The redirect error happens because the rules are evaluated again //after// the redirect is performed. So, if you access ''/'', and it gets redirected to e.g. ''/us'', then on ''/us'', it will be asked to redirect //again// to ''/us'' - and you end up with a loop. The fix is to add an additional condition to prevent this, such as ''RewriteCond %{REQUEST_URI} !^/us[NA]''. This way, you only redirect to ''/us'' if the country code from GeoIP matches US //and// the request URI doesn't already start with ''/us''. | ||
The final rules should be: | The final rules should be: |