Skip to content

Commit d53d222

Browse files
committed
Generate redirects from an environment variable
Used for redirecting city domains (which point to the same Netlify deployment) to their city in the country domain instead, to not have duplicate content under multiple domains. [skip ci]
1 parent 99622cd commit d53d222

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

nuxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export default defineNuxtConfig({
7070
defaultImport: 'component'
7171
},
7272
runtimeConfig: {
73+
REDIRECT_RULES: process.env.REDIRECT_RULES,
7374
public: {
7475
GOOGLE_MAPS_API: process.env.GOOGLE_MAPS_API,
7576
SUPA_KEY: process.env.SUPA_KEY,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default defineEventHandler(async (event) => {
2+
if (!useRuntimeConfig().REDIRECT_RULES) return
3+
4+
const url = getRequestURL(event)
5+
6+
/**
7+
* This expects a value of this format:
8+
* `targetdomain.com,citydomain1.com:cityname1,citydomain2.com:cityname2`
9+
*/
10+
const [targetdomain, ...redirects] = useRuntimeConfig().REDIRECT_RULES.split(',')
11+
12+
const defaultLocale = useRuntimeConfig().public.DATO_DEFAULT_LOCALE || 'en'
13+
14+
for (const redirect of redirects) {
15+
const [citydomain, cityname] = redirect.split(':')
16+
17+
if (url.hostname === citydomain.trim()) {
18+
await sendRedirect(event, `https://${targetdomain.trim()}/${defaultLocale}/cities/${cityname.trim()}`, 301)
19+
break;
20+
}
21+
}
22+
})

0 commit comments

Comments
 (0)