Skip to content

Commit ef16df6

Browse files
committed
Fix bug in dynamic redirects
1 parent deac048 commit ef16df6

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

worker/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,18 @@ export default {
7373
const redirects: Redirects = await redirectsResponse.json();
7474

7575
for (const redirect of redirects) {
76-
if (
77-
(redirect.kind === "static" && requestUrl.pathname === redirect.from)
78-
|| (redirect.kind === "dynamic" && requestUrl.pathname.startsWith(redirect.from))
79-
) {
76+
if (redirect.kind === "static" && requestUrl.pathname === redirect.from) {
8077
const url = new URL(request.url);
8178
url.pathname = redirect.to;
8279
return Response.redirect(url.toString(), redirect.status);
8380
}
81+
82+
if (redirect.kind === "dynamic" && requestUrl.pathname.startsWith(redirect.from)) {
83+
const url = new URL(request.url);
84+
const suffix = requestUrl.pathname.slice(redirect.from.length);
85+
url.pathname = redirect.to + suffix;
86+
return Response.redirect(url.toString(), redirect.status);
87+
}
8488
}
8589

8690
const response = await env.ASSETS.fetch(request);

0 commit comments

Comments
 (0)