Keep query string intact during directory redirect

This is a different fix to use the escaping mechanisms of `url.format` for the search string, and includes a test!

Fixes #792
Fixes #793