我在重定向到 HTTPS 以便在 Windows 服务器上工作时遇到了麻烦
它运行的是 微软互联网资讯服务的第6版
used to working with Apache on a Linux host so I turned to the Internet for
帮助,这是排名最高的堆栈溢出问题时,我搜索
但是,所选择的答案不起作用
为了我。
After some trial and error, I discovered that with IIS, $_SERVER['HTTPS'] is
set to off for non-TLS connections. I thought the following code should
帮助任何其他 IIS 用户谁来到这个问题通过搜索引擎。
On my AWS beanstalk server, I don't see $_SERVER['HTTPS'] variable. I do see $_SERVER['HTTP_X_FORWARDED_PROTO'] which can be either 'http' or 'https' so if you're hosting on AWS, use this:
if ($_SERVER['HTTP_HOST'] != 'localhost' and $_SERVER['HTTP_X_FORWARDED_PROTO'] != "https") {
$location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $location);
exit;
}