Build multi-language routing without cloaking mishaps.
- User
Build multi-language routing without cloaking mishaps.
Assistant
Build multi-language routing without cloaking mishaps.
1. Understand the Goal: The user wants to implement multi-language routing (e.g., example.com/en/, example.com/fr/) but is specifically concerned about avoiding "cloaking mishaps." This means I need to provide a solution that serves the same content to users and search engines, avoids redirects based on IP (which Google considers cloaking if done improperly), and uses SEO best practices like hreflang tags.
2. Analyze the Core Problem:
3. Determine the Best Approach (Strategy):
/en/, /es/) are easiest to manage and best for SEO.Accept-Language headers for bots. Ideally, even for users, use a modal/banner ("Do you want to switch to Spanish?") rather than a hard redirect, or at least allow the override via URL parameters.<head> management (hreflang).Content-Language).4. Draft the Solution Steps:
googlebot to a different URL than what was requested.hreflang tags. This is crucial.5. Refining the "Anti-Cloaking" Advice:
site.com/en from France.site.com/fr.site.com/en. Javascript detects browser language is French. Shows a toast: "View in French?". User clicks "Yes", then redirect happens./fr version if it starts at /en. Or, if it requests /fr and you redirect it to /en because of its IP, that's cloaking.6. Structuring the Output:
hreflang implementation.7. Drafting the Content (Iterative refinement):
/fr, serve French. Ignore headers. If path is root /, then maybe look at headers, but better to default to English or use a 302 redirect only if no preference is set, but strictly never redirect bots. Actually, the safest bet for the root is to show a "gateway" page or default language + a cookie.Let's refine the "Root" redirect logic because that's where most mistakes happen.
example.com.example.com/en based on Accept-Language. (302 is temporary, implies the root is the canonical source).8. Final Polish:
Make sure to emphasize that hreflang is the bridge that tells Google "These pages are the same content in different languages," so you don't get hit for duplicate content.
Key Points to cover:
/en/, /fr/).Let's write the response following this structure. I will provide a conceptual implementation (Middleware) and the specific SEO tags needed.
Self-Correction during drafting: Make sure to mention that Googlebot can render Javascript, but server-side logic is safer for indexing. If using client-side redirects for root paths, keep them simple.
Let's organize into:
This looks solid. Building multi-language routing is standard; doing it without getting penalized for "cloaking" requires strict discipline.
Cloaking (showing different content to Googlebot than to users) usually happens accidentally in multi-language setups when developers rely too heavily on IP detection (Geolocation) or Accept-Language headers to force redirects.
Here is the architecture for a Search Engine Friendly (SEF) multi-language routing system that avoids cloaking traps.
To avoid cloaking, the content served at a specific URL must be static regardless of who is asking for it.
/about. Server checks IP, sees France, redirects to /fr/about./about. Server serves English content. There is a manual link to switch to /fr/about.Here is the implementation strategy.
Use Subdirectories (e.g., example.com/en/, example.com/fr/) combined with a Root Default.
example.com -> Root (The "Global" entry point).example.com/en/ -> English content.example.com/fr/ -> French content.The biggest cloaking risk is Server-Side Redirects based on IP/Headers.
Googlebot usually crawls from the US with English headers. If you force-redirect a bot visiting example.com/fr/ back to example.com/en/ because its IP is American, you have effectively cloaked the French page (it exists for French users, but not for Google).
The Safe Logic (Node.js/Express Example):
const express = require('express');
const app = express();
// Supported languages
const LANGS = ['en', 'fr', 'es'];
const DEFAULT_LANG = 'en';
app.use((req, res, next) => {
// 1. Parse URL
const pathSegments = req.path.split('/');
const langPrefix = pathSegments[1]; // e.g., 'fr' from '/fr/products'
// 2. Check if URL explicitly requests a language
if (LANGS.includes(langPrefix)) {
// EXPLICIT REQUEST: Serve that language regardless of user IP or browser headers.
// This prevents cloaking. If Google asks for /fr/, give it /fr/.
req.lang = langPrefix;
req.url = req.url.replace(`/${langPrefix}`, ''); // Strip lang for internal routing
next();
} else {
// 3. Handle ROOT (example.com/) logic
// WARNING: This is where cloaking happens.
// OPTION A (Safest): Always serve default language at root.
req.lang = DEFAULT_LANG;
next();
// OPTION B (Risky - IP Based):
// If you must redirect at root, ensure you are NOT redirecting search engines.
// This is hard to do perfectly. Stick to Option A and use CSS/JS banners for user switching.
}
});
// Route Handler
app.get('/products', (req, res) => {
// req.lang contains the active language
res.send(`Serving products in ${req.lang}`);
});
hreflang)Since you aren't forcing redirects, you need to tell search engines that the pages are related. You do this via Hreflang tags.
If you serve example.com/en/products and example.com/fr/products, both pages must contain the following in their <head>:
<link rel="alternate" hreflang="en" href="https://example.com/en/products" />
<link rel="alternate" hreflang="fr" href="https://example.com/fr/products" />
<link rel="alternate" hreflang="x-default" href="https://example.com/en/products" />
/fr/ URL. A user in Germany searches in English; Google serves the /en/ URL. This is localization, not cloaking.Since you are not force-redirecting users on the server side, how do you help users who land on the wrong page?
You implement a Client-Side Language Suggestion Banner.
example.com/en (English).const userPref = navigator.language; (Returns fr-FR).<a href="/fr/current-page">Switch to French</a>.Why this is safe: Googlebot usually doesn't execute complex navigator logic or click buttons, but even if it sees the banner, it's just a link. It isn't being redirected against its will.
/)The root URL (example.com) is the most dangerous for cloaking.
/. You detect IP is German. You redirect to /de/. Later, Googlebot hits / from US IP. You redirect to /us/. Google sees / as a moving target.example.com a simple language selector or a landing page that explains "Choose your region."x-default: In your sitemap and headers, designate the root or the main language (usually English) as hreflang="x-default"./fr/ serve French content to everyone (including US IPs and Googlebot)? (Yes = Safe).<link rel="alternate"> tags on every page pointing to the other languages? (Yes = Safe).