Rather than adding extra logic to the template and code that is very dependent on specific strings I would suggest one of the following:

a. always render `req.headers.referer` into the page or
b. add en event listener on the client that redirects to `document.referrer` or `history.go(-1)`, although i'm not sure the latter works in ie9-