next js multi subdomains
Unanswered
Tonkinese posted this in #help-forum
TonkineseOP
i have website in next js which has domain abc.com
what i want is when someone visit to my website
my website will check location of user and get country code
then it will rewrite my url to abc.com/country_code
Abc.com/country_code should use default paths
How i can implement this i tried many things but non of them work
what i want is when someone visit to my website
my website will check location of user and get country code
then it will rewrite my url to abc.com/country_code
Abc.com/country_code should use default paths
How i can implement this i tried many things but non of them work
9 Replies
You can follow this guide inside the docs, to create a powerful internationalation: https://nextjs.org/docs/app/building-your-application/routing/internationalization#terminology
@Tonkinese
@Tonkinese
You will also able to choose if you want to do it via subdomains e.g.:
example.de/
example.fr/
example.com/
…
Or doing it inside the path:
example.com/de
example.com/fr
example.com/en
example.de/
example.fr/
example.com/
…
Or doing it inside the path:
example.com/de
example.com/fr
example.com/en
TonkineseOP
Internationalisation already in the project i just want to prefix country code after domain url on base of user country from his ip countrycode will be prefix in all routes
TonkineseOP
@B33fb0n3 can you suggest some code i tried it but didnt work
@Tonkinese <@301376057326567425> can you suggest some code i tried it but didnt work
Yea, I do this one:
let locales = ['en-US', 'nl-NL', 'nl']
// Get the preferred locale, similar to the above or using a library
function getLocale(request) { ... }
export function middleware(request) {
// Check if there is any supported locale in the pathname
const { pathname } = request.nextUrl
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(
)
if (pathnameHasLocale) return
// Redirect if there is no locale
const locale = getLocale(request)
request.nextUrl.pathname =
// e.g. incoming request is /products
// The new URL is now /en-US/products
return Response.redirect(request.nextUrl)
}
export const config = {
matcher: [
// Skip all internal paths (_next)
'/((?!_next).*)',
// Optional: only run on root (/) URL
// '/'
],
}
Inside the getLocale you can choose if you want to get the language based on the user browser preferences or depending on the location or whatever
let locales = ['en-US', 'nl-NL', 'nl']
// Get the preferred locale, similar to the above or using a library
function getLocale(request) { ... }
export function middleware(request) {
// Check if there is any supported locale in the pathname
const { pathname } = request.nextUrl
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(
/${locale}/) || pathname === /${locale})
if (pathnameHasLocale) return
// Redirect if there is no locale
const locale = getLocale(request)
request.nextUrl.pathname =
/${locale}${pathname}// e.g. incoming request is /products
// The new URL is now /en-US/products
return Response.redirect(request.nextUrl)
}
export const config = {
matcher: [
// Skip all internal paths (_next)
'/((?!_next).*)',
// Optional: only run on root (/) URL
// '/'
],
}
Inside the getLocale you can choose if you want to get the language based on the user browser preferences or depending on the location or whatever
@Tonkinese worked?
@Tonkinese ?