htaccess rewrite rules with mod_GeoIP

2019-04-18 19:56发布

问题:

I'm having some issues with mod_geoip for an ecommerce site that has 3 different stores. We have our main store at root/store, but also have have stores at root/ukstore and root/austore. The main root/store contains a /skin, /media, and /js directories that contain all css, images, and javascripts. With this current setup, the urls flawlessly swap out the base, but leaves the rest of the URL intact so if a user from the UK goes to root/store/category/product/ they get redirected to root/ukstore/category/product/.

2 things that are issues now.

Whenever a customer accesses a secure page like checkout or account, the URLs for css and javascript are being rewritten to root/ukstore/skin or root/ukstore/js. Is there something I'm missing in regards to SSL for those URLs?

Secondly, if a user from South Africa accesses the store, they are rewritten to the UK store and all the css, js, images are perfectly linked back to store/skin, but if a user from the United Kingdom accesses the store then the URLs for css, js, and images are trying to be rewritten to root/ukstore/skin

Here is the code in my htaccess file in root/store. Each store also has their own htaccess file, but without much in there.

<IfModule mod_geoip.c>
GeoIPEnable On

Options +FollowSymLinks
RewriteEngine on
#skip processing directories
RewriteRule ^store/skin/  - [L,NC]
RewriteRule ^store/media/  - [L,NC]
RewriteRule ^store/js/  - [L,NC]

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(AQ|AU|MY|BV|BN|BN|MM|KH|CN|CX|CC|CK|GQ|FJ|PF|GU|GW|HM|HK|ID|KI|KR|KP|KR|LA|MO|MY|MH|FM|MM|NR|NC|PG|NZ|NU|NF|PG|CN|PH|PN|WS|SG|SB|KR|LK|BN|TW|TW|AU|TH|TL|TK|TO|TV|VU|VN|VN|WF)$
RewriteCond %{REQUEST_URI} ^/store(/.*)$ [NC]
RewriteRule ^ /austore%1 [L,R]

#UK Store Rewrites
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(AF|AX|AL|DZ|AD|AO|AM|AT|AZ|BH|BD|IS|BY|BE|BJ|BT|BA|BW|IO|BG|BF|BI|GB|CM|ES|CV|CF|TD|GB|KM|CG|CD|CG|CD|CI|HR|CY|CZ|CZ|BJ|DK|DJ|EG|ER|EE|ET|FO|FI|FR|TF|GA|GM|GE|DE|GH|GI|GD|GR|GL|GN|VA|HU|IS|IN|IR|IR|IQ|IE|IL|IT|CI|JO|KZ|KE|KW|KG|LV|LB|LS|LR|LY|LY|LI|LT|LU|MK|MG|MW|MV|ML|MT|MR|MU|MC|MC|MN|ME|MA|MZ|NA|NP|NL|NE|NG|IE|NO|OM|PK|PS|PS|CG|PL|PT|QA|CI|MK|ZA|CD|RE|RO|RU|RW|SH|SM|ST|SA|SN|RS|SC|SL|SK|SI|SO|SO|ZA|ES|SD|SJ|SZ|SE|CH|SY|SY|TJ|TZ|TN|TR|TM|AE|UG|UA|AE|GB|BF|UZ|VA|GB|EH|YE|ZM|ZW)$
RewriteCond %{REQUEST_URI} ^/store(/.*)$ [NC]
RewriteRule ^ /ukstore%1 [L,R]
</IfModule>

Any help would be greatly appreciated!

回答1:

The problem is that you are using the rewrite conditions in a .htaccess context so the conditions should be relative to the directory you are in and that is the reason why the rewrite conditions relative to the skin, js and media mentioned in the question do not match. You should replace them with something like:

RewriteRule ^(skin|media|js)/ - [L,NC]

or, as mentioned in the mod_rewrite guide ( http://httpd.apache.org/docs/current/mod/mod_rewrite.html#RewriteRule )

If you wish to match against the full URL-path in a per-directory (htaccess) RewriteRule, use the %{REQUEST_URI} variable in a RewriteCond.



回答2:

Ip-based redirection could fail and what usually people want is language-based redirection. You can read the language configured in the user browser and just redirect them to a language that your web has. Just like that:

RewriteCond %{HTTP:Accept-Language} ^(en|ja|de|zh|fr|it|nl|fi|sv|no) [NC]
RewriteRule ^$ /en [R=303,L,QSA]

And what you want in your approach is to have a direct access to the static files in your server, this can be achieved setting this condition, so static files don't get rewrited

RewriteCond %{REQUEST_FILENAME} !-f