Normalise handling of redirecting to HTTP_REFERER

We have differing logic on how to check the referrer for safety, which
should be bundled in one place. Plus it's annoying to have to remember
the typo in REFERER.

@aaugustin helpfully pointed us towards is_safe_url(), so that's the
best thing to use anyway.

Fixes #1221.