Skip to content

Commit 94f2a4f

Browse files
committed
fix: keep clean redirects on canonical https
1 parent fdabdab commit 94f2a4f

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

‎src/server/cache-control.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { APIContext } from 'astro'
2+
import { getEnv } from './env'
23

34
export function isPublicPage(context: APIContext) {
45
if (context.request.method !== 'GET' && context.request.method !== 'HEAD') {
@@ -24,5 +25,16 @@ export function cleanUrlRedirect(context: APIContext) {
2425
if (!url.search) return null
2526

2627
url.search = ''
28+
const site = getEnv('PUBLIC_SITE_URL')
29+
if (site) {
30+
try {
31+
const origin = new URL(site)
32+
url.protocol = origin.protocol
33+
url.host = origin.host
34+
} catch {
35+
// Keep the request URL when PUBLIC_SITE_URL is not a valid URL.
36+
}
37+
}
38+
2739
return Response.redirect(url, 301)
2840
}

0 commit comments

Comments
 (0)