سؤال

I use this mobile htaccess redirection code:

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} (mobile|android|iphone|ipod|ipad|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|iris|kindle|sgh|brew|htc|j2me|lg|midp|mot|netfront|nokia|obigo|openweb|operamini|palm|plucker|pocket|psp|samsung|sanyo|sch|smartphone|sonyericsson|symbian|symbos|teleca|up.browser|vodafone|wap|webos|windowsce|xda|xiino) [NC]
RewriteRule ^(.*)$ url1 [R=302,L]

Now, I want to keep that code, but also to redirect mobile users from some specific countries to different url2. How can I do this? Is the code below is the solution /is this possible both codes to exists in htaccess/?

RewriteEngine On - /for all users/

RewriteCond %{HTTP_USER_AGENT} (mobile|android|iphone|ipod|ipad|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|iris|kindle|sgh|brew|htc|j2me|lg|midp|mot|netfront|nokia|obigo|openweb|operamini|palm|plucker|pocket|psp|samsung|sanyo|sch|smartphone|sonyericsson|symbian|symbos|teleca|up.browser|vodafone|wap|webos|windowsce|xda|xiino) [NC]
RewriteRule ^(.*)$ url1 [R=302,L]

RewriteEngine On - /only for Ca, US and MX for example/

RewriteCond %{HTTP_USER_AGENT} (mobile|android|iphone|ipod|ipad|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|iris|kindle|sgh|brew|htc|j2me|lg|midp|mot|netfront|nokia|obigo|openweb|operamini|palm|plucker|pocket|psp|samsung|sanyo|sch|smartphone|sonyericsson|symbian|symbos|teleca|up.browser|vodafone|wap|webos|windowsce|xda|xiino) [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CA|US|MX)$
RewriteRule ^(.*)$ url2 [R=302,L]

Thank you.

هل كانت مفيدة؟

المحلول

First, you need to make sure you've installed and loaded the mod_geoip module (more info here). Then you want your rules to be the other way around, since the first rule would simply match all countries, redirect, and the second rule will never be reached:

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} (mobile|android|iphone|ipod|ipad|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|iris|kindle|sgh|brew|htc|j2me|lg|midp|mot|netfront|nokia|obigo|openweb|operamini|palm|plucker|pocket|psp|samsung|sanyo|sch|smartphone|sonyericsson|symbian|symbos|teleca|up.browser|vodafone|wap|webos|windowsce|xda|xiino) [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CA|US|MX)$
RewriteRule ^(.*)$ url2 [R=302,L]

RewriteCond %{HTTP_USER_AGENT} (mobile|android|iphone|ipod|ipad|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|iris|kindle|sgh|brew|htc|j2me|lg|midp|mot|netfront|nokia|obigo|openweb|operamini|palm|plucker|pocket|psp|samsung|sanyo|sch|smartphone|sonyericsson|symbian|symbos|teleca|up.browser|vodafone|wap|webos|windowsce|xda|xiino) [NC]
RewriteRule ^(.*)$ url1 [R=302,L]

Alternatively to using GEOIP_COUNTRY_CODE, you could also use:

RewriteCond %{ENV:GEOIP_CONTINENT_CODE} ^NA$

for all of North America.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top