In 3.1.6.1 the `~` was handled properly (either literally or with
`%E7`). This broke in 3.1.6.2, which used URI encoding in both
cases and didn't escape the `\` before `%E7`. This patch restores
the former behavior: `~` isn't escaped if it isn't escaped in the
original URL, and if it is, a backslash is added before `%`.
Closes#9043.