"/" in the uri must never be urlencoded

or it breaks opening files completely.

also remove some code repetition.