You may want to use wp_get_referer() in this case.
On my side, I also redirect on a dynamic uri. Since I have multiple cases and origins, I had to introduce the notion of Sessions. I use for this the Simple Session Support plugin, which works fine. I can then use the super-global $_SESSION array in which I simply set a new $_SESSION['specific_referer'] variable.
I set this variable with the correct value, using wp_get_referer() or any other source, depending on my specific context. Then I use OneAll Social Login filters this <way:
/*** Forcing redirect to $_SESSION['specific_referer'] after registration & login in OneAll Social Login ***/
add_filter('oa_social_login_filter_registration_redirect_url', 'my_login_registration_redirect_filter', 10, 2);
add_filter('oa_social_login_filter_login_redirect_url', 'my_login_registration_redirect_filter', 10, 2);
function my_login_registration_redirect_filter ($url, $user_data)
Btw, I also use this function in other redirect filters, since I rely upon Theme-my-Login and s2Member plugins, too.