Rewrite remote URL checking to avoid potential infinite loop.

@cheister I don't expect this would have caused problems on this code base, but changing the algorithm to be consistent with the one in kochiku.