I believe that the issue is in here as new JUri doesn't contain index.php (you remove it in line 585+).