{"id":1561,"date":"2013-08-02T15:42:44","date_gmt":"2013-08-02T19:42:44","guid":{"rendered":"http:\/\/cd34.com\/blog\/?p=1561"},"modified":"2013-08-02T15:42:44","modified_gmt":"2013-08-02T19:42:44","slug":"apache2-using-mod_rewrite-and-rewritemap-with-a-python-program-to-georedirect","status":"publish","type":"post","link":"https:\/\/cd34.com\/blog\/webserver\/apache2-using-mod_rewrite-and-rewritemap-with-a-python-program-to-georedirect\/","title":{"rendered":"Apache2 &#8211; Using mod_rewrite and RewriteMap with a Python program to georedirect"},"content":{"rendered":"<p>Almost every machine we run has geoip loaded and clients are able to access the country code of a surfer without having to write too much code. One client decided that they wanted to redirect users from certain states to another page to deal with sales tax and shipping issues but had a mixture of perl scripts and php scripts running their shopping cart.<\/p>\n<p>Having done something very similar using mod_rewrite and rewritemap with a text file, the simple solution appeared to be a short Python script to interface with the <a href=\"http:\/\/dev.maxmind.com\/geoip\/legacy\/geolite\/\" target=\"_blank\">GeoIPLite data<\/a> and use mod_rewrite.<\/p>\n<p>In our VirtualHost config, we put:<\/p>\n<pre>\r\nRewriteMap statecode prg:\/var\/www\/rewritemap\/tools\/rewritemap.py\r\n<\/pre>\n<p>To set up our Python environment, we did:<\/p>\n<pre>\r\nvirtualenv \/var\/www\/rewritemap\r\ncd \/var\/www\/rewritemap\r\nsource bin\/activate\r\ngit clone https:\/\/github.com\/maxmind\/geoip-api-python.git\r\ncd geoip-api-python\r\npython setup.py install\r\ncd ..\r\nmkdir tools\r\ncd tools\r\nwget -N http:\/\/geolite.maxmind.com\/download\/geoip\/database\/GeoLiteCity.dat.gz\r\ngunzip GeoLiteCity.dat.gz\r\n<\/pre>\n<p>rewritemap.py<\/p>\n<pre>\r\n#!\/var\/www\/rewritemap\/bin\/python\r\n\r\nimport sys\r\n\r\nimport GeoIP\r\ngi = GeoIP.open(\"\/var\/www\/rewritemap\/tools\/GeoLiteCity.dat\", \\\r\n         GeoIP.GEOIP_STANDARD)\r\n\r\nSTATE = 'NULL'\r\ngir = gi.record_by_name(sys.stdin.readline())\r\nif gir != None:\r\n  if gir['country_code'] == 'US':\r\n    STATE = gir['region']\r\n\r\nprint STATE\r\n<\/pre>\n<p>Then, in our .htaccess we put:<\/p>\n<pre>\r\nRewriteEngine on\r\nRewriteCond ${statecode:%{REMOTE_ADDR}|NONE} ^(PA|DC|CO)$\r\nRewriteRule .* \/specialpage\/ [R=302,L]\r\n<\/pre>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/webserver\/apache2-using-mod_rewrite-and-rewritemap-with-a-python-program-to-georedirect\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Almost every machine we run has geoip loaded and clients are able to access the country code of a surfer without having to write too much code. One client decided that they wanted to redirect users from certain states to another page to deal with sales tax and shipping issues but had a mixture of [&hellip;]<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/webserver\/apache2-using-mod_rewrite-and-rewritemap-with-a-python-program-to-georedirect\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[181,294,182,299],"class_list":["post-1561","post","type-post","status-publish","format-standard","hentry","category-webserver","tag-apache2","tag-geoip","tag-mod_rewrite","tag-python"],"_links":{"self":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/1561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/comments?post=1561"}],"version-history":[{"count":1,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/1561\/revisions"}],"predecessor-version":[{"id":1562,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/1561\/revisions\/1562"}],"wp:attachment":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/media?parent=1561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/categories?post=1561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/tags?post=1561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}