As discussed, instead of relying on referrer, it might be easier to explicitly pass in a return_url in the first instance, ie from a hidden input field.