How to wildcard redirect in javascript

How to i want to wildcard redirect in javascript. I placed this code in index.html but the wildcard redirect did not work. When I access the rootdomain.com it redirects to test.example.com But when I access the rootdomain.com/path it doesn’t redirect to test.example.com/path

<script>

    setInterval(function(){ 
        number = document.getElementById("number").innerHTML;
        var url1 = 'test.';
        var url2 = 'example';
        var url3 = '.com';
        if (number > 0) {
            number--;
        }
        document.getElementById("number").innerHTML = number; 
        if (number == 0) {
            window.location.replace('https://' + url1 + url2 + url3 + + window.location.pathname);
        }
     }, 1000);
    
</script>

I tried a simple code, but still did not work:

<script>
    window.setTimeout(function(){

        // Move to a new location or you can do something else
        window.location.href = "https://test.example.com" + window.location.pathname;

    }, 5000);

</script>


<h1>Wait 5seconds</h1>

Thank You

Answer

Try it:

window.location = 'https://' + url1 + url2 + url3 +   window.location.host+window.location.pathname;