0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-17 23:11:29 -05:00

fix: extra safety checks for the base path redirect

This commit is contained in:
Tony Sullivan 2022-04-20 17:06:34 +02:00
parent 49934fa565
commit 4755ce9dba

View file

@ -112,17 +112,26 @@ async function handle404Response(
html = subpathNotUsedTemplate(devRoot, pathname);
} else {
// HACK: redirect without the base path for assets in publicDir
if (config.base && config.base !== './' && !pathname.startsWith(config.base)) {
console.log('REDIRECT::', pathname, config.base, pathname.replace(config.base, ''));
// Only redirect if:
// (1) astroConfig.base was provided
// (2) pathname begins with the base path
// (3) pathname isn't `{basepath}` or `{basepath}/`
const baseRegex = new RegExp(`${config.base}\/\d*$`);
const shouldRedirect = config.base && config.base !== './'
&& pathname.match(baseRegex);
if (shouldRedirect) {
const response = new Response(null, {
status: 301,
headers: {
Location: pathname.replace(config.base, ''),
// substring at 1 to maintain the leading /
Location: pathname.replace(config.base.substring(1), ''),
},
});
await writeWebResponse(res, response);
return;
}
html = notFoundTemplate({
statusCode: 404,
title: 'Not found',