Try to detect and keep the encoding used for the URL

Not ready for merge as I am unsure whether the URL passed into the RedirectResponse should already be URL-encoded or not. (If it should be, we might need to fix the matcher as well for GH #7411.)