PHP 重定向相同地址不同端口

PHP Redirect Same Address Different Port

现在我有这个:

header("Refresh: 0; url=http://192.168.100.100:10500/redirect2.php");

如何做同样的重定向但不写地址,只写端口?这两个文件都在同一台服务器上的同一文件夹中。

问题是我不知道将用于访问此服务器的地址(私有或 public)。

使用超全局$_SERVER数组,Locationheader和exit;

$port = '10500';
header('Location: '
    . ($_SERVER['HTTPS'] ? 'https' : 'http')
    . '://' . $_SERVER['HTTP_HOST'] . ':' . $port
    . $_SERVER['REQUEST_URI']);
exit;

也许对某些人有用,isHttps() 函数可以检测 SSL。

function isHttps() {
      return
        (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
        || $_SERVER['SERVER_PORT'] == 443;
    }

$destinationPort = '10500';

$schema = isHttps() ? "https" : "http";
$schema .= "://";
header('Location: ' . $schema . $_SERVER['HTTP_HOST'] . ':' . $port
    . $_SERVER['REQUEST_URI']);

exit;