url redirection (on error 404, cloudfront redirect to this html file containing this script)

#385
Raw
Author
Anonymous
Created
Sept. 15, 2021, 12:30 p.m.
Expires
Never
Size
985 bytes
Hits
44
Syntax
JavaScript
    <script>
        var pathSplit = location.pathname.split('/')
        var hashFragment = location.hash;
        if (pathSplit.length === 1) {
            alert('No path');
        }
        if (pathSplit.length === 2) {
            window.location.replace(location.origin + '/' + pathSplit[1] + '/index.html' + hashFragment);
            alert('path has 2 parts; pathspit.lenght == 2');
        } else {
            if (pathSplit[2] === '') {
                window.location.replace(location.origin + '/' + pathSplit[1] + '/index.html' + hashFragment);
                alert('pathSplit[2] === stringEmpty');
            } else {
                window.location.replace(location.origin + '/' + pathSplit[1] + '/index.html?redirect=' + pathSplit.slice(2, pathSplit.length).join('/') + hashFragment);
                alert('pathSplit[2] != stringEmpty');
            }
        }
    </script>