"/" in the uri must never be urlencoded, it breaks opening files completely.
also remove some code repetition.