Properly handle invalid URLs