Obtain bad URL from Safari's current tab with Applescript
I've tried numerous times to reproduce the issue, however I couldn't. So this solution was tested by manually entering a standard URL twice, via copy and paste, and going from there. I also used the bad URL you've shown in your question. 

In Safari, this essentially tries to build a valid URL from of the double URL using AppleScript run as an Automator Service when pressing ⌘B and sets the current document (window, tab) to the proper URL.

Create an Automator Service with the settings as shown in the image below and save it as: Fix Bad URL

Then in System Preferences > Keyboard > Shortcuts > Services, scroll down to General, select Fix Bad URL, click Add Shortcut and type: ⌘B





AppleScript code for the Automator Service:

on badURL()
    try
        --    # Put the bad URL on the Clipboard.
        tell application "Safari"
            activate
            delay 0.5
            tell application "System Events"
                key code 37 using {command down} # ⌘L
                delay 0.5
                key code 8 using {command down} # ⌘C
                delay 0.5
            end tell
        end tell
        --    # Retrieve the bad URL from the Clipboard.
        set theBadURL to get (the clipboard)
        --    # Trim the first eight characters off of 'theBadURL'.
        --    # This is done because the bad URL can have an occurrence of both 'http' and 'https' in the string in either
        --    # order and by trimming off, it lessens the amount of logic necessary to build a good URL from the bad URL.
        --    # The good URL, after all, is going to be built from what's after the second occurrence of either one. 
        --    # Test first for 'https' then 'http', as that makes more sense logically to do so.            
        set theBadURL to do shell script "awk 'BEGIN { print substr(\"" &amp; theBadURL &amp; "\", 9) }'"
        --    # Build the good URL from the bad URL.
        set theGoodURL to do shell script "awk -F 'https.*//' '{print $2}'&lt;&lt;&lt;" &amp; quoted form of theBadURL
        if theGoodURL is not equal to "" then
            set theGoodURL to "https://" &amp; theGoodURL
        else
            set theGoodURL to do shell script "awk -F 'http.*//' '{print $2}'&lt;&lt;&lt;" &amp; quoted form of theBadURL
            if theGoodURL is not equal to "" then
                set theGoodURL to "http://" &amp; theGoodURL
            end if
        end if
        return theGoodURL
    on error eStr number eNum
        display dialog eStr &amp; " number " &amp; eNum buttons {"OK"} default button 1 with icon caution
        return
    end try
end badURL

on run
    try
        tell application "Safari"
            activate
            set thisTabsURL to (get URL of current tab of window 1)
            --    # Check for bad URL and if found, build a good URL from it.
            tell current application
                if thisTabsURL contains "file:" then
                    set theGoodURL to my badURL()
                    tell application "Safari" to set the URL of the front document to theGoodURL
                end if
            end tell
        end tell
    on error eStr number eNum
        display dialog eStr &amp; " number " &amp; eNum buttons {"OK"} default button 1 with icon caution
        return
    end try
end run




Note that while I tested this under OS X 10.8.5 and macOS 10.12 and it did work under my test conditions, nonetheless it may not work properly every time under every condition and hence why the try and on error statements are being used in the code. Hopefully this will trap any error(s) with appropriate output to then enhance the code to handle any error(s) that didn't occur during my testing.