Well, you still have not shown me an actual example URL, so here's my best guess:
$host = (!empty($_SERVER['HTTP_HOST'])) ?
$_SERVER['HTTP_HOST'] :
$_SERVER['SERVER_NAME'];
$path = dirname($_SERVER['SCRIPT_NAME']);
$request = "http://$host".$_SERVER['REQUEST_URI'];
$url = "http://$host$path/?url=".urlencode($request);
header("Location: $url");