Skip to content

Commit 828791d

Browse files
docs-botCopilot
andauthored
Fix .md URLs without language prefix redirecting to 404 (#60996)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 229c102 commit 828791d

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

src/frame/tests/server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ describe('server', () => {
324324
expect(res.body).toMatch(/^# .+/)
325325
})
326326

327+
test('.md URL without language prefix redirects to /en/ equivalent', async () => {
328+
const res = await get('/get-started.md')
329+
expect(res.statusCode).toBe(302)
330+
expect(res.headers.location).toBe('/en/get-started.md')
331+
})
332+
327333
test('/index.md redirects to the page without /index.md', async () => {
328334
const res = await get('/en/get-started/index.md')
329335
expect(res.statusCode).toBe(302)

src/redirects/middleware/handle-redirects.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ export default function handleRedirects(req: ExtendedRequest, res: Response, nex
107107
// But for example, a `/authentication/connecting-to-github-with-ssh`
108108
// needs to become `/en/authentication/connecting-to-github-with-ssh`
109109
const possibleRedirectTo = `/en${req.path}`
110+
// Pages are keyed without .md, so strip it before lookup
111+
const lookupPath = possibleRedirectTo.endsWith('.md')
112+
? possibleRedirectTo.replace(/\.md$/, '')
113+
: possibleRedirectTo
110114
if (!req.context.pages) throw new Error('req.context.pages not yet set')
111-
if (possibleRedirectTo in req.context.pages || isDeprecatedVersion(req.path)) {
115+
if (lookupPath in req.context.pages || isDeprecatedVersion(req.path)) {
112116
const language = getLanguage(req)
113117

114118
// Note, it's important to use `req.url` here and not `req.path`
@@ -127,7 +131,10 @@ export default function handleRedirects(req: ExtendedRequest, res: Response, nex
127131

128132
// do not redirect if the redirected page can't be found
129133
if (
130-
!(req.context.pages[removeQueryParams(redirect)] || isDeprecatedVersion(req.path)) &&
134+
!(
135+
req.context.pages[removeQueryParams(redirect).replace(/\.md$/, '')] ||
136+
isDeprecatedVersion(req.path)
137+
) &&
131138
!redirect.includes('://')
132139
) {
133140
// display error on the page in development, but not in production

0 commit comments

Comments
 (0)