Skip to content

Commit 7009a9d

Browse files
committed
#3335 External and Internal Redirects
1 parent fa32b1c commit 7009a9d

File tree

2 files changed

+152
-2
lines changed

2 files changed

+152
-2
lines changed

web/common/helpers/redirects.ts

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { sanityFetch } from '@/sanity/lib/live'
2+
import { externalRedirects, redirects } from '@/sanity/queries/redirects'
3+
4+
export const getRedirectUrl = async (slug: string, locale: string) => {
5+
const { data } = await sanityFetch({
6+
query: redirects,
7+
params: {
8+
slug: slug,
9+
slugWithLocale: `/${locale}${slug}`,
10+
},
11+
})
12+
return data
13+
}
14+
15+
export const getExternalRedirectUrl = async (slug: string, locale: string) => {
16+
const { data } = await sanityFetch({
17+
query: externalRedirects,
18+
params: {
19+
slug: slug,
20+
slugWithLocale: `/${locale}${slug}`,
21+
},
22+
})
23+
return data
24+
}
25+
26+
export const getWWWRedirect = (
27+
requestLocale: string,
28+
host: string,
29+
pathname: string,
30+
): string | undefined => {
31+
if (!host.includes('www')) {
32+
return `https://www.${host}/${requestLocale}${pathname}`
33+
}
34+
return undefined
35+
}
36+
37+
export const getDnsRedirect = (host: string, pathname: string) => {
38+
const dns = host
39+
.replace('http://', '')
40+
.replace('https://', '')
41+
.replace('www.', '')
42+
43+
if (dns === 'statoil.com') {
44+
return `https://www.equinor.com${pathname}`
45+
}
46+
47+
if (dns === 'equinor.kr') {
48+
return `https://www.equinor.co.kr${pathname}`
49+
}
50+
51+
const redirect =
52+
dnsRedirects.find(redirect => redirect.from === dns + pathname) ||
53+
dnsRedirects.find(redirect => redirect.from === dns)
54+
55+
return redirect && `https://www.equinor.com${redirect.to}`
56+
}
57+
58+
const dnsRedirects = [
59+
{
60+
from: 'equinor.co.uk/mariner',
61+
to: '/en/energy/mariner',
62+
},
63+
{
64+
from: 'equinor.co.uk',
65+
to: '/en/where-we-are/united-kingdom',
66+
},
67+
{
68+
from: 'equinorpensjon.no',
69+
to: '/no/om-oss/equinor-pensjon',
70+
},
71+
{
72+
from: 'statoilpensjon.no',
73+
to: '/no/om-oss/equinor-pensjon',
74+
},
75+
{
76+
from: 'equinor.co.tz',
77+
to: '/en/where-we-are/tanzania',
78+
},
79+
{
80+
from: 'statoil.co.tz',
81+
to: '/en/where-we-are/tanzania',
82+
},
83+
{
84+
from: 'equinor.no',
85+
to: '/no',
86+
},
87+
{
88+
from: 'equinorventures.com',
89+
to: '/en/energy/ventures',
90+
},
91+
{
92+
from: 'h2hsaltend.co.uk',
93+
to: '/en/energy/h2h-saltend',
94+
},
95+
{
96+
from: 'equinor.ca',
97+
to: '/en/where-we-are/canada',
98+
},
99+
{
100+
from: 'techstars.equinor.com',
101+
to: '/en/energy/techstars',
102+
},
103+
{
104+
from: 'equinor.com.au',
105+
to: '/en/where-we-are/australia',
106+
},
107+
{
108+
from: 'digitalfieldworker.equinor.com',
109+
to: '/energy/digitalfieldworker',
110+
},
111+
{
112+
from: 'eu2nsea.com',
113+
to: '/energy/eu2nsea',
114+
},
115+
{
116+
from: 'eu2nsea.eu',
117+
to: '/energy/eu2nsea',
118+
},
119+
{
120+
from: 'morgendagenshelter.no',
121+
to: '/no/om-oss/sponsing-og-stotte',
122+
},
123+
{
124+
from: 'data.equinor.com',
125+
to: '/energy/data-sharing',
126+
},
127+
]

web/proxy.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { type NextRequest, NextResponse } from 'next/server'
22
import createMiddleware from 'next-intl/middleware'
3+
import {
4+
getExternalRedirectUrl,
5+
getRedirectUrl,
6+
} from './common/helpers/redirects'
37
import { routing } from './i18n/routing'
4-
/* import { getLocaleFromName } from './sanity/localization' */
58
/* import { getDocumentBySlug } from './sanity/queries/paths/getPaths' */
69
import archivedNews from './lib/archive/archivedNewsPaths.json'
710
import { Flags } from './sanity/helpers/datasetHelpers'
11+
import { getLocaleFromName } from './sanity/helpers/localization'
812
/* eslint-disable @typescript-eslint/ban-ts-comment */
913
import { getDnsRedirect, getWWWRedirect } from './sanity/interface/redirects'
1014

@@ -79,7 +83,7 @@ export async function proxy(request: NextRequest) {
7983

8084
// Skip WWW redirect for Radix URLs and localhost
8185
if (!wwwExcludedDomains.includes(host)) {
82-
const wwwRedirect = getWWWRedirect(locale,host, pathname)
86+
const wwwRedirect = getWWWRedirect(locale, host, pathname)
8387
if (wwwRedirect) {
8488
return NextResponse.redirect(wwwRedirect, PERMANENT_REDIRECT)
8589
}
@@ -114,6 +118,25 @@ export async function proxy(request: NextRequest) {
114118
)
115119
}
116120

121+
// Check if an external redirect exists in sanity
122+
const externalRedirect = await getExternalRedirectUrl(
123+
pathname,
124+
request.nextUrl.locale,
125+
)
126+
if (externalRedirect) {
127+
return NextResponse.redirect(externalRedirect.to, PERMANENT_REDIRECT)
128+
}
129+
130+
// Check if an internal redirect exists in sanity
131+
const redirect = await getRedirectUrl(pathname, request.nextUrl.locale)
132+
if (redirect) {
133+
const locale = getLocaleFromName(redirect.lang)
134+
return NextResponse.redirect(
135+
`${origin}/${locale}${redirect.to !== '/' ? redirect.to : ''}`,
136+
PERMANENT_REDIRECT,
137+
)
138+
}
139+
117140
// Check if pathname ends with .html
118141
if (isDotHtml) {
119142
return NextResponse.redirect(

0 commit comments

Comments
 (0)