Adding some logging to see whats happening.
Checking if resulting URL is valid before fixing it. Otherwise go to the original URL.