我的 5 个网站有 5 个域,但我只有一个主机,所以我在该主机中托管了我的 5 个网站(例如 site1 的 URL 是 192.168.xx.xx/site1/index.php, site2 的网址是 192.168.xx.xx/site2/index.php)。 我需要使我的索引.php(192.168.xx.xx/index.php)使用用户的请求自动重定向网站。我尝试了此代码,但它不正确,请帮助我。
<?php
$host=$_SERVER['SERVER_NAME'];
header("'Location:http://exampledomain.com/' . "$host"");?>
代码的问题是串联。在 PHP 中,.
表示连接。您正在使用.
但变量周围有不必要的其他引号。
所以你的代码:
header("'Location:http://exampledomain.com/' . "$host"");?>
应该是:
header("Location:http://exampledomain.com/" . $host);?>
header
命令参考中还有一个示例可能对您非常有帮助:
<?php
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/''');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>
PHP 代码无效。
这是有效的:
$host = $_SERVER['SERVER_NAME'];
header("Location:http://exampledomain.com/{$host}");
exit;
您的串联无效:
header("'Location:http://exampledomain.com/' . "$host"");?>
它应该是:
header("Location:http://exampledomain.com/" . $host);?>
exit;
另外,请记住在header()
后添加exit;
以停止 PHP 执行,否则它将继续执行。