用PHP从HTTP重定向到HTTPS

时间:2022-11-23 18:05:34

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进行重定向?

4 个解决方案

#1


176  

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

尝试以下方法(应该适用于Apache和IIS):

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

#2


9  

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();
}
?>

#3


5  

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重写规则。

#4


3  

Redirecting from HTTP to HTTPS with PHP on IIS

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.

在Windows服务器上运行MS Internet Information Services (IIS)的第6版时,我很难重定向到HTTPS。我更习惯于在Linux主机上使用Apache,所以我求助于Internet,这是我搜索“php将http重定向到https”时遇到的*别堆栈溢出问题。然而,选择的答案对我不起作用。

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 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").

编辑:从另一个堆栈溢出回答,一个更简单的解决方案是检查是否($_SERVER["HTTPS"] != "on")。

#1


176  

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

尝试以下方法(应该适用于Apache和IIS):

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

#2


9  

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();
}
?>

#3


5  

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重写规则。

#4


3  

Redirecting from HTTP to HTTPS with PHP on IIS

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.

在Windows服务器上运行MS Internet Information Services (IIS)的第6版时,我很难重定向到HTTPS。我更习惯于在Linux主机上使用Apache,所以我求助于Internet,这是我搜索“php将http重定向到https”时遇到的*别堆栈溢出问题。然而,选择的答案对我不起作用。

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 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").

编辑:从另一个堆栈溢出回答,一个更简单的解决方案是检查是否($_SERVER["HTTPS"] != "on")。