Skip to content

Commit

Permalink
⚡ Set brazil ssl to radix ssl automation #2539 (#2608)
Browse files Browse the repository at this point in the history
  • Loading branch information
VarunVAshrit authored Oct 30, 2024
1 parent 40f7ce0 commit ef1ac68
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 7 additions & 0 deletions web/common/helpers/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export const getExternalRedirectUrl = async (slug: string, locale: string): Prom
return getClient(false).fetch(externalRedirects, { slug: slug, slugWithLocale: `/${locale}${slug}` })
}

export const getWWWRedirect = (host: string, pathname: string): string | undefined => {
if (!host.includes("www")) {
return `https://www.${host}${pathname}`;
}
return undefined;
};

export const getDnsRedirect = (host: string, pathname: string) => {
const dns = host.replace('http://', '').replace('https://', '').replace('www.', '')

Expand Down
9 changes: 7 additions & 2 deletions web/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { getRedirectUrl, getDnsRedirect, getExternalRedirectUrl } from './common/helpers/redirects'
import { getRedirectUrl, getDnsRedirect, getExternalRedirectUrl, getWWWRedirect } from './common/helpers/redirects'
import { NextRequest, NextResponse } from 'next/server'
import { getLocaleFromName } from './lib/localization'
import { Flags } from './common/helpers/datasetHelpers'
Expand Down Expand Up @@ -50,14 +50,19 @@ export async function middleware(request: NextRequest) {
if ((PUBLIC_FILE.test(pathname) && !isDotHtml) || pathname.includes('/api/')) {
return undefined
}

// Check if it is a DNS redirect
const host = String(request.headers.get('host'))
const dnsRedirect = getDnsRedirect(host, pathname)
if (dnsRedirect) {
return NextResponse.redirect(dnsRedirect, PERMANENT_REDIRECT)
}

// Check if it is a WWW redirect
const wwwRedirect = getWWWRedirect(host, pathname)
if (wwwRedirect) {
return NextResponse.redirect(wwwRedirect, PERMANENT_REDIRECT)
}

// Redirect external links to news which is now archived if link doesn't exist in Sanity
if (Flags.HAS_ARCHIVED_NEWS && pathname.startsWith('/news') && !pathname.startsWith('/news/archive')) {
const existsInSanity = await pathExistsInSanity(pathname, isPreview)
Expand Down

0 comments on commit ef1ac68

Please sign in to comment.