0

我正在尝试重定向到 https,但它不适用于我将其放在 index.php 开头的代码

 <?php 
if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == ""){
   $redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
   header("Location: $redirect");

 }
4

1 回答 1

0

尝试以下方法之一:

.htaccess

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

PHP的

if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
    if(!headers_sent()) {
        header("Status: 301 Moved Permanently");
        header(sprintf(
            'Location: https://%s%s',
            $_SERVER['HTTP_HOST'],
            $_SERVER['REQUEST_URI']
        ));
        exit();
    }
}
于 2013-11-02T07:24:42.193 回答