שאלו את שאלתכם וקבלו סיכום של המסמך על ידי הפניה לדף הזה ולספק ה-AI שתבחרו
היסטוריית גרסאות
- "Narrow the return type of getLocalizedUrl and getLocalizedPath from routing.domains, and emit domain-aware sitemap alternates."v9.4.022.8.2026
- "Add domain-based locale routing via routing.domains configuration."v8.7.03.4.2026
התוכן של הדף הזה תורגם באמצעות AI.
צפייה בגרסה האחרונה של התוכן המקורי באנגליתאם יש לכם רעיון לשיפור התיעוד הזה, אל תהססו לתרום על ידי הגשת Pull Request ב-GitHub.
קישור GitHub לתיעודהעתקת ה-Markdown של המסמך ללוח
Custom Domains
Intlayer supports domain-based locale routing, allowing you to serve specific locales from dedicated hostnames. For example, Chinese visitors can be directed to intlayer.zh instead of intlayer.org/zh.
How It Works
The domains map in routing associates each locale with a hostname. Intlayer uses this map in two places:
- URL generation (
getLocalizedUrl): when the target locale lives on a different domain than the current page, an absolute URL is returned (e.g.https://intlayer.zh/about). When both domains match, a relative URL is returned (e.g./fr/about). - Server proxy (Next.js & Vite): incoming requests are redirected or rewritten based on the domain they arrive on.
Exclusive vs. shared domains
The key distinction is exclusivity:
- Exclusive domain - only one locale maps to that hostname (e.g.
zh → intlayer.zh). The domain itself identifies the locale, so no locale prefix is added to the path.https://intlayer.zh/aboutserves Chinese content. - Shared domain - multiple locales map to the same hostname (e.g.
enandfrboth map tointlayer.org). Normal prefix-based routing applies.intlayer.org/fr/aboutserves French content.
Configuration
העתקת הקוד ללוח
Locales that are not listed in domains continue to use the standard prefix routing without any domain override.
URL Generation
getLocalizedUrl automatically produces the correct URL type based on the calling context.
Same-domain locale (relative URL)
העתקת הקוד ללוח
Cross-domain locale (absolute URL)
העתקת הקוד ללוח
Serving from the locale's own domain
העתקת הקוד ללוח
Current domain auto-detection
currentDomain is optional. When omitted, getLocalizedUrl resolves it in this order:
- The hostname of an absolute input URL (e.g.
https://intlayer.org/about→intlayer.org). window.location.hostnamein browser environments.- If neither is available (SSR, static generation), the absolute URL on the locale's domain is returned - this is the safe fallback. A relative URL could not be trusted there: the prefix of an exclusive-domain locale is already stripped, so it would resolve to the wrong locale on whichever domain happens to serve the page.
העתקת הקוד ללוח
getMultilingualUrls with domains
getMultilingualUrls calls getLocalizedUrl for every locale, so it produces a mix of relative and absolute URLs depending on the caller's domain:
העתקת הקוד ללוח
These absolute URLs are ready to use in <link rel="alternate" hreflang="..."> tags for SEO.
Type Inference
routing.domains is serialized into the generated module augmentation, so
getLocalizedUrl and getLocalizedPath narrow their return type to the exact
URL a domain-routed locale resolves to, the prefix suppression included.
העתקת הקוד ללוח
The URL type is the union of the two values the function can return: the
absolute URL on the locale's domain, and the relative one it returns when the
page being rendered already lives on that domain. getLocalizedPath has no such
ambiguity - it never emits an origin - so it stays a single literal.
Locales that share a domain keep their normal prefix in the type as well:
העתקת הקוד ללוח
Regenerate the types (
npx intlayer build, or any dev server run) after changingrouting.domains- the narrowing comes from the generated__RoutingRegistry, not from the config file itself.
Proxy Behaviour
Next.js
The intlayerProxy middleware handles domain routing automatically. Add it to your middleware.ts:
העתקת הקוד ללוח
Redirect - request arrives on the wrong domain for a given locale prefix:
העתקת הקוד ללוח
Rewrite - request arrives on the locale's exclusive domain without a prefix:
העתקת הקוד ללוח
Vite
The intlayerProxy Vite plugin applies the same logic during development:
Since Intlayer v9,intlayerProxy()is bundled directly into theintlayer()plugin and enabled by default through therouting.enableProxyoption (trueby default). Registering it separately as shown below is now optional. It is kept for backward compatibility and for setups that need to control plugin order. Setrouting.enableProxy: falseto opt out. See the v9 release notes.
העתקת הקוד ללוח
Note: in local development you are typically onlocalhost, so cross-domain redirects will point to the live domains rather than another local port. Use a hosts-file override (e.g.127.0.0.1 intlayer.zh) or a reverse proxy if you need to test multi-domain routing locally.
Locale Switcher
The useLocale hook from next-intlayer handles domain-aware navigation automatically. When a user switches to a locale on a different domain the hook performs a full-page navigation (window.location.href) instead of a client-side router push, because the Next.js router cannot cross origins.
העתקת הקוד ללוח
No extra configuration is required - useLocale detects window.location.hostname internally and decides between router.replace (same domain) and window.location.href (cross-domain).
SEO: hreflang Alternate Links
Domain-based routing is commonly used together with hreflang to tell search engines which URL to index for each language. Use getMultilingualUrls to generate the full set of alternate URLs:
העתקת הקוד ללוח
This produces:
העתקת הקוד ללוח
Core Utilities
פתיחת הטבלה בחלון כדי לראות את כל תוכן הנתונים בבירור
| Utility | Description |
|---|---|
getLocalizedUrl(url, locale, { currentDomain }) | Returns relative or absolute URL depending on whether the target locale is on the current domain. |
getMultilingualUrls(url, { currentDomain }) | Returns a locale-keyed map of localized URLs, mixing relative and absolute as needed. |
getPrefix(locale, { domains }) | Returns an empty prefix for exclusive-domain locales, normal prefix otherwise. |
getLocalizedPath(path, locale, { domains }) | Returns the path served on the locale's domain, without its prefix - never an origin. |
generateSitemap(entries, { domains }) | Emits hreflang alternates pointing at each locale's own domain. |
