使用 PHP 从 HTTP 重定向到 HTTPS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5106313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:50:31  来源:igfitidea点击:

Redirecting from HTTP to HTTPS with PHP

phphttps

提问by Psyche

I'm working on a shopping cart website and I would like to redirect the user to a HTTPS page when he's entering his billing details and maintain the HTTPS connection for the next pages until he logs out.

我正在购物车网站上工作,我想在用户输入账单详细信息时将用户重定向到 HTTPS 页面,并在他注销之前维护下一页的 HTTPS 连接。

What do I need to install on the server (I'm using Apache) in order to do this, and how can this redirect be done from PHP?

为了做到这一点,我需要在服务器上安装什么(我正在使用 Apache),以及如何从 PHP 完成这种重定向?

回答by Raphael Michel

Try something like this (should work for Apache and IIS):

尝试这样的事情(应该适用于 Apache 和 IIS):

if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off") {
    $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . $location);
    exit;
}

回答by Matiasg1982

This is a good way to do it:

这是一个很好的方法:

<?php
if (!(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || 
   $_SERVER['HTTPS'] == 1) ||  
   isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&   
   $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))
{
   $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   header('HTTP/1.1 301 Moved Permanently');
   header('Location: ' . $redirect);
   exit();
}
?>

回答by Anthony Geoghegan

Redirecting from HTTP to HTTPS with PHP on IIS

在 IIS 上使用 PHP 从 HTTP 重定向到 HTTPS

I was having trouble getting redirection to HTTPS to work on a Windows server which runs version 6 of MS Internet Information Services (IIS). I'm more used to working with Apache on a Linux host so I turned to the Internet for help and this was the highest ranking Stack Overflow question when I searched for “php redirect http to https”. However, the selected answer didn't work for me.

我无法重定向到 HTTPS 以在运行MS Internet Information Services (IIS)版本 6 的 Windows 服务器上工作。我更习惯于在 Linux 主机上使用 Apache,所以我转向 Internet 寻求帮助,这是当我搜索“php redirect http to https”时排名最高的 Stack Overflow 问题。但是,选定的答案对我不起作用。

After some trial and error, I discovered that with IIS, $_SERVER['HTTPS']is set to offfor non-TLS connections. I thought the following code should help any other IIS users who come to this question via search engine.

经过一些试验和错误,我发现 IIS$_SERVER['HTTPS']设置为off用于非 TLS 连接。我认为以下代码应该可以帮助通过搜索引擎来解决此问题的任何其他 IIS 用户。

<?php
if (! isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off' ) {
    $redirect_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header("Location: $redirect_url");
    exit();
}
?>

Edit: From another Stack Overflow answer, a simpler solution is to check if($_SERVER["HTTPS"] != "on").

编辑:从另一个Stack Overflow 答案中,一个更简单的解决方案是检查if($_SERVER["HTTPS"] != "on")

回答by powtac

You can always use

您可以随时使用

header('Location: https://www.domain.com/cart_save/');

to redirect to the save URL.

重定向到保存 URL。

But I would recommend to do it by .htaccess and the Apache rewrite rules.

但我建议通过 .htaccess 和 Apache 重写规则来完成。

回答by phoenix

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:

在我的 AWS beanstalk 服务器上,我没有看到 $_SERVER['HTTPS'] 变量。我确实看到 $_SERVER['HTTP_X_FORWARDED_PROTO'] 可以是“http”或“https”,所以如果您在 AWS 上托管,请使用:

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;
}