Managing a multi-regional web product often introduces tricky architectural overhead, especially when you want to avoid maintaining multiple separate repositories. Recently, we faced the challenge of scaling a high-performance content and affiliate platform tailored for two distinct markets in Africa: Kenya and Ghana (casinoguide-kenya.com and casinoguide-ghana.com).
Instead of duplicating code or dealing with messy subpaths like /ke/ or /gh/, we implemented a clean apex-domain architecture powered by a single Next.js 16 codebase. Here is a breakdown of how we engineered the edge routing, state-sharing, and SEO pipelines to make this setup production-ready.
1. Edge Routing via Next.js Middleware
To serve entirely different regional content while keeping public URLs clean (users should see root paths on their respective domains), we intercepted requests directly at the edge inside middleware.ts.
The middleware detects the incoming hostname, maps it to the corresponding region, handles www to non-www redirection, and transparently rewrites the request internally to dedicated App Router directories (/kenya/... or /ghana/...):
import { NextRequest, NextResponse } from 'next/server';
export function middleware(request: NextRequest) {
const hostname = request.headers.get('host') || '';
const pathname = request.nextUrl.pathname;
// 1. Enforce apex domain structure
if (hostname.startsWith('www.')) {
const apexHost = hostname.replace('www.', '');
return NextResponse.redirect(`https://${apexHost}${pathname}`, 301);
}
// 2. Resolve region based on domain
const region = hostname.includes('kenya') ? 'kenya' : 'ghana';
// 3. Internal path rewriting
const rewriteUrl = request.nextUrl.clone();
rewriteUrl.pathname = `/${region}${pathname === '/' ? '' : pathname}`;
return NextResponse.rewrite(rewriteUrl);
}
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};
This keeps our file tree cleanly separated on the backend:
-
src/app/ghana/**— Pages and features specific to Ghana. -
src/app/kenya/**— Pages and features specific to Kenya. -
src/config/regions/— Region-specific metadata, UI configurations, brand assets, and payment methods (like M-Pesa vs. MTN MoMo).
2. Bulletproof Multi-Regional SEO Pipeline
Multi-tenant sites frequently crash and burn in search engine rankings if crawlers get confused about localization. To prevent duplicate content flags, we structured three core layers into our Next.js layout and metadata engines:
-
Dynamic
hreflangMapping: Utilizing Next.jsgenerateMetadata, every page programmatically injects accurate canonical and cross-region alternate headers (en-KEanden-GH), ensuring search engines link the mirrored pages properly. -
Dynamic HTML Language Attributes: Instead of hardcoding a generic
lang="en"in the root layout, our layout reads the active request context and renderslang="en-KE"orlang="en-GH"dynamically. - Host-Aware Sitemaps: Our sitemap generator reads the active domain dynamically at runtime, outputting clean, market-specific XML indexes.
3. Shared UI Components with Isolated Data
A major win of this setup is DRY code. We maintain over 50+ modular UI components under src/components/ui/ (such as structured data tables, accordion blocks, and layout wrappers).
These components are completely stateless regarding geographic data. They rely entirely on typed configuration modules:
// Reusable component rendering market-specific data sets
import { TopList } from '@/components/ui/TopList';
import { getKenyaCasinos } from '@/config/regions/kenya';
export default async function KenyaOffersPage() {
const data = await getKenyaCasinos();
return <TopList items={data} region="kenya" />;
}
By decoupling markup from localized JSON configs, we eliminated redundant code while tailoring content precisely to local user intent.
Conclusion
Leveraging Next.js 16 App Router together with Edge Middleware provides a robust foundation for multi-tenant applications. If you're building a multi-country platform, setting up clean edge routing and strict SEO metadata generation from day one will save your team massive refactoring efforts down the road.
Check out our live production implementations for regional guides at CasinoGuide Kenya and CasinoGuide Ghana.
Top comments (0)