If you already know some information about the URL you can use that information to replace the respective part.
Start with an empty path
<?php
$path = "";
?>
Check for the protocol (ie. http, cgi)
If you know the protocol is http you can replace this accordingly.
<?php
// SERVER_PROTOCOL is in the form "protocol/rev#", this strips the revision
$path .= strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/");
?>
Check if secure (HTTPS)