{"id":5235,"date":"2020-07-23T13:47:33","date_gmt":"2020-07-23T08:17:33","guid":{"rendered":"https:\/\/www.skynats.com\/?p=5235"},"modified":"2024-10-28T12:48:25","modified_gmt":"2024-10-28T07:18:25","slug":"country-based-redirection-in-magento-using-geoip","status":"publish","type":"post","link":"https:\/\/www.skynats.com\/blog\/country-based-redirection-in-magento-using-geoip\/","title":{"rendered":"Country Based Redirection In Magento using GeoIP"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The GeoIP module is used to find which Geolocation or area is used by the IP from IP location database. We can setup country based redirection rule in htaccess for Magento2 which will redirect the required stores to the specific countries.<br>To add rules for country based redirection in Magento we have to install the GeoIP module. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below installation needs to be done on a centos 7 server and if you need this to be done from our end or need help to setup on any other OS, Click <a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/www.skynats.com\/contact-us\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>here<\/strong><\/a> to contact our experts to get it done right now.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">Install GeoIP and dependencies<\/span><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yum install GeoIP GeoIP-devel GeoIP-data zlib-devel<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Change the directory to the \/usr\/share\/GeoIP\/, upload and extract GeoIP database in this location<br>You can download the GeoIP database from <a href=\"https:\/\/dev.maxmind.com\/geoip\/geoip2\/geolite2\/\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.maxmind.com\/geoip\/<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd \/usr\/share\/GeoIP\/\ntar -xzvf GeoLite2-ASN_20200630.tar.gz<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">Install the required packages for GeoIP<\/span><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yum install httpd-devel apr-devel ea-apache24-devel.x86_64 ea-apache24-tools.x86_64<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">&nbsp;Download and extract GeoIP module using the below steps<\/span><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget https:\/\/github.com\/maxmind\/geoip-api-mod_geoip2\/archive\/1.2.10.tar.gz\ntar -xzvf 1.2.10.tar.gz\ncd mod_geoip2_1.2.10\nsed s\/remote_ip\/client_ip\/g -i mod_geoip.c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">Compile and install the GeoIP module<\/span><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apxs -i -a -L\/usr\/lib64 -I\/usr\/include -lGeoIP -c mod_geoip.c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">Load the module in Apache at Apache modules conf.<\/span><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/etc\/apache2\/conf.modules.d\/mod_geoip.conf\n\n###########\nLoadModule geoip_module \/usr\/lib64\/apache2\/modules\/mod_geoip.so\n\n&lt;IfModule mod_geoip.c&gt;\nGeoIPEnable On\nGeoIPScanProxyHeaders On\nGeoIPDBFile \/usr\/share\/GeoIP\/GeoIPASNum.dat Standard\nGeoIPDBFile \/usr\/share\/GeoIP\/GeoIPCity.dat Standard\n&lt;\/IfModule&gt;\n###########<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">Check Apache Configuration and restart Apache<\/span><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>httpd -t\nservice httpd restart<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">Verify if GeoIP module is enabled by<\/span><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>httpd -M | grep geoip<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we have to add the rule for redirection of countrywide access in Magento2 site. There are certain country codes available for GeoIP database. We can use that country codes here to add rules in .htaccess.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;IfModule mod_rewrite.c&gt;\nRewriteEngine on\nRewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(GB|RU|IN)$\nRewriteCond %{REQUEST_URI} !^\/store\/\nRewriteRule .* https:\/\/www.example.com\/store\/ &#91;L]\n&lt;\/IfModule&gt;\n\nwhere GB|RU|IN are the country codes for UK, Russia and India<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">Restart Apache<\/span><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>service httpd restart<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">According to the above rule, the users accessing from UK and Russia and India for \/store of https:\/\/www.example.com, will redirect to https:\/\/www.example.com\/store.<br>By using GeoIP module we can also block and deny countrywide access to the site.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote is-style-default has-vivid-red-color has-text-color\"><blockquote><p><strong>Are you struggling with managing your server?<\/strong><\/p><cite><em><strong><a href=\"https:\/\/www.skynats.com\/cpanel-server-management\/\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">check our management plans here<\/a><\/strong><\/em><\/cite><\/blockquote><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>The GeoIP module is used to find which Geolocation or area is used by the IP from IP location database. We can setup country based redirection rule in htaccess for Magento2 which will redirect the required stores to the specific countries.To add rules for country based redirection in Magento we have to install the GeoIP [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,68],"tags":[42,91,92,35,93,94,18,95,96,97,98,99,100,72,22],"class_list":["post-5235","post","type-post","status-publish","format-standard","hentry","category-blog","category-server-management","tag-apache","tag-apache-country-block","tag-apache-redirection","tag-apache-webserver","tag-apache-website","tag-countrybased-redirection","tag-cpanel","tag-htaccess-redirects","tag-htaccess-rules","tag-magento","tag-magento-county-block","tag-magento-redirects","tag-magento2","tag-server-management","tag-server-support"],"_links":{"self":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/5235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/comments?post=5235"}],"version-history":[{"count":0,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/5235\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/media?parent=5235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/categories?post=5235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/tags?post=5235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}