If I understood you right, you may need this..
$uri = isset($_SERVER['REQUEST_URI']) ? strip_tags($_SERVER['REQUEST_URI']) : '';
$urlvariables = (substr($uri, 0, 1) == "https://stackoverflow.com/") ? substr($uri, 1) : $uri;
$variables = explode("https://stackoverflow.com/", $uri);
echo $whatyouneed = $_SERVER['HTTP_HOST'] . "https://stackoverflow.com/" . $variables['1'];
You may give it a try.
solved Get real base URL