Skip to content

Commit 0f83719

Browse files
rachmariheiskr
andauthored
Fix 500 error when redirecting asset paths that start with // (#54872)
Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
1 parent c11b848 commit 0f83719

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/redirects/middleware/handle-redirects.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ import { ExtendedRequest, URLSearchParamsTypes } from '@/types'
1010
export default function handleRedirects(req: ExtendedRequest, res: Response, next: NextFunction) {
1111
if (!req.context) throw new Error('Request not contextualized')
1212

13+
// Any double-slashes in the URL should be removed first
14+
// This must be done before checking if the path
15+
// is an asset (patterns.assetPaths)
16+
if (req.path.includes('//')) {
17+
return res.redirect(301, req.path.replace(/\/+/g, '/'))
18+
}
19+
1320
// never redirect assets
1421
if (patterns.assetPaths.test(req.path)) return next()
1522

1623
// All /api/ endpoints handle their own redirects
1724
// such as /api/pageinfo redirects to /api/pageinfo/v1
1825
if (req.path.startsWith('/api/')) return next()
1926

20-
// Any double-slashes in the URL should be removed first
21-
if (req.path.includes('//')) {
22-
return res.redirect(301, req.path.replace(/(\/+)/g, '/'))
23-
}
24-
2527
// blanket redirects for languageless homepage
2628
if (req.path === '/') {
2729
const language = getLanguage(req)

0 commit comments

Comments
 (0)