Well, it's a lot more difficult than you might think. Here are some of the issues you run into:
- IP addresses are updated on a continued basis. And free GeoIP databases have huge chunks of data missing so accuracy can be a big problem.
- Internal pages need to be dealt with. It's easy to redirect someone on the home page, but what about if they land on an internal page? You have to add cookie logic so they can be redirected on the first visit in a session, and then leave them alone as they check out the site.
- Caching is so necessary nowadays that you need to have a system that treats identifies each user. You don't want one visitor from Florida going to the Florida page and then every visitor after that.
- Requests for data with every user on every page can really slow down your server. You need to save each user session so that you don't have to keep looking up the information over and over.
Each week of use brought more and more issues so I finally gave up and did some research. Thankfully, a company already identified and took care of these issues with a service, GeotargetingWP. GeotargetingWP is a powerful API service to geotarget content or create geo targeted redirects within WordPress. They've built four plugins that can be used depending on your needs:
- Geotargeting Pro is the favorite plugin for affiliate marketers for their country specific offers because of it's simplicity and powerful features. Now with premium accuracy to help you target States and Cities specific content.
- Geo Redirects sends users to different web sites based on their location with a few simple steps. Geo Redirects plugin for WordPress it's a powerful tool that will let you easily create a redirect based on multiple criteria.
- Geo Flags is a simple addon for the Geotargeting Pro plugin that will let you display the current user country flag or any other flag that you want by using a simple shortcode like this:
[geo-flag squared="false" size="100px"]
- Geo blocker plugin for WordPress will let you easily block access to users from certain locations. You can block them from accessing your entire site or simple choose which pages.
The platform also allows you to build and utilize regions to target so that you don't have to create infinite rules based on multiple regions. You can group countries or cities in order to make it easier to target users. As an example, you could create a region called Europe and another called America, and then simple use those names in shortcodes or widgets saving you time. Caching isn't an issue, either. They detect real user IP no matter if you use Cloudflare, Sucuri, Akamai, Ezoic, Reblaze, Varnish, etc. If you have something custom it can be easily added.
Their API provides top geolocation accuracy, returning continent, country, state and city data. Since the cost is based on usage, you can just connect directly to their API and use it as you'd like.
Disclosure: We're using our affiliate link in this post since we like the service so much!