Links in stage environments are absolute and send users to live enviroment This problem has been driving crazy for years. 
whenever I create a stage environment of a site, I copy the database from the production site. some of the links there - whether in the menus or in the content or in the code, are absolute paths in the production site.
so i have links that lead users from one environment to the other.

Ive been looking for a way to alter this behaviour.
so that all link as 
live-environment.com/some-page ----> stage-environment.com/some-page

I dont care if it will happen before the render or just after the click. just that the end result will be that the user stays in the stage environment.

How can I do this. (drupal 7)

Thanks a lot for your help
So ive solved it with js 

$('a').each(function() {
    if(!$(this)||$(this)=="undefined") return;
    var href = $(this).attr('href');
    if(!href)return;
    if (href.includes("live-environment.org")){
        var live_env_url = href;
        var stg_url = new URL(live_env_url);
        stg_url.hostname = Drupal.settings.basePath.substring(0, Drupal.settings.basePath.length-1);;
        $(this).attr('href', decodeURIComponent(stg_url.href));
    }
});