Skip to content

Commit c89c1ef

Browse files
authored
Prevent URL redirect looping in 404 page
Added check to prevent URL redirect looping in 404 handler.
1 parent b8ad2df commit c89c1ef

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

dist/404.html

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,19 @@
2626
(() => {
2727
const pathSegmentsToKeep = 0;
2828
let l = new URL(window.location);
29-
l.pathname = '/' + l.pathname.split('/').slice(1, 1 + pathSegmentsToKeep).join('/') + '/?/' +
30-
l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~');
31-
l.search = l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '';
32-
window.SPA404Redirect = l;
33-
window.location.replace(l.href);
29+
30+
// Check if the URL is already in redirect format to prevent looping
31+
if (!l.search.startsWith('?/')) {
32+
const basePath = '/' + l.pathname.split('/').slice(1, 1 + pathSegmentsToKeep).join('/');
33+
const restPath = l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/');
34+
const newPath = basePath + (basePath.endsWith('/') ? '' : '/') + '?/' + restPath.replace(/&/g, '~and~');
35+
const newSearch = l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '';
36+
37+
l.pathname = newPath;
38+
l.search = newSearch;
39+
40+
window.SPA404Redirect = l;
41+
window.location.replace(l.href);
42+
}
3443
})();
3544
</script></head><body></body></html>

0 commit comments

Comments
 (0)