Improve bad URL handling

This prevents a segfault when a specific kind of bad formatting is given for a URL.

Fixes #1921.