How to GeoRedirect Visitors by Country: Complete Guide
Every method for country-based redirects compared: server-side, client-side JavaScript, and edge-side. Includes code examples, pros and cons, and key implementation tips for SEO safety.

Redirecting visitors based on their country is one of the most common geo-targeting tasks. Whether you're sending users to localized storefronts, language-specific pages, or region-restricted content, the goal is the same: get the right visitor to the right page automatically. Here's every method available in 2026.
Method 1: Server-side redirects
The traditional approach. Your server looks up the visitor's IP, resolves it to a country, and returns a 302 redirect before any content loads. Here's what it looks like in Node.js with a MaxMind database:
const geoip = require('geoip-lite');
app.use((req, res, next) => {
const geo = geoip.lookup(req.ip);
if (geo?.country === 'FR') {
return res.redirect(302, '/fr/');
}
next();
});Pros: No page flicker. Full control over redirect logic. Works with any backend.
Cons: Requires a server you control (rules out static sites and most Shopify setups). MaxMind database needs regular updates. Adds latency if your server is far from the visitor.
Method 2: Client-side redirects (JavaScript)
A JavaScript snippet calls a geolocation API, then redirects via window.location. This works on any platform, including static sites and Shopify:
fetch('https://api.ipgeolocation.io/ipgeo?apiKey=KEY')
.then(r => r.json())
.then(data => {
if (data.country_code2 === 'DE') {
window.location.href = '/de/';
}
});Pros: Works on any website. No server changes needed.
Cons: Visible page flicker as the original page loads before redirecting. External API dependency. Blocked by ad blockers. Terrible for SEO — search engines may not execute the JavaScript.
Method 3: Edge-side redirects
The modern approach. CDN edge workers (Cloudflare Workers, Vercel Edge Middleware, AWS CloudFront Functions) intercept requests before they reach your origin server. The redirect happens in the data center closest to the visitor, in under 10 milliseconds:
// Cloudflare Worker
export default {
async fetch(request) {
const country = request.cf?.country;
if (country === 'JP') {
return Response.redirect(
'https://example.com/jp/', 302
);
}
return fetch(request);
}
};Pros: Fastest possible redirects. No flicker. No external API calls. Geolocation data is built into the CDN.
Cons: Requires CDN configuration or a tool that manages it for you.
The easiest option: use a geo redirect tool
If you don't want to write code or manage infrastructure, GeoSwap's GeoRedirect handles everything. Add a script tag, set your rules in the dashboard, and redirects happen at the edge. It uses the same Cloudflare Workers technology as Method 3, but you configure it through a visual interface instead of writing code.
GeoSwap also handles the details that custom implementations often miss: bot detection for SEO safety, 302 status codes by default, and warnings when you create exclusion rules that might affect search engines.
Key implementation tips
- Always use 302 redirects for geo-based routing. 301s tell search engines the redirect is permanent, which is wrong for conditional redirects.
- Provide a way to override: Let users manually switch regions. Some visitors use VPNs or travel frequently.
- Use explicit rules: Target specific countries rather than excluding countries. This keeps Googlebot from getting caught in your redirects.
- Test from multiple locations: Use a VPN or GeoSwap's preview mode to verify your rules work correctly.
Country-based redirects are straightforward in concept but tricky in execution. Edge-side redirects are the gold standard for speed and reliability — and with GeoSwap, you get them without writing a line of code, completely free. Read our geo redirect documentation for the full setup guide, or check your current redirects with our redirect checker.
