Make URL-building logic more resilient against reverse proxy misconfiguration (#1085)