php重定向代码http到https

标签 php redirect

我想将我的 www.mysite.in 重定向到 https://www.mysite.in ,我在 index.php 中尝试了所有可能的 php 代码,如下所述,但仍然无法重定向....我没有得到确切的解决方案。请帮助我。

<?php
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != '') {
    header("location: https://".$_SERVER['HTTP_HOST']);
} else {
    header("location: http://".$_SERVER['HTTP_HOST'])
}?>
------------------------------------OR---------------------------------------
<?php

if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == ""){
    $redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: $redirect");
}
?>
------------------------------------OR----------------------------------------
<?php
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "")
    {
        $HTTPURI = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        header("HTTP/1.1 301 Moved Permanently"); // Optional.
        header("Location: $HTTPURI"); 
        exit(0);
    }
?>

最佳答案

这样怎么样:

具有检查网站协议(protocol)的功能:

function is_https() {
    if (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 1) {
        return TRUE;
    } elseif (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') {
        return TRUE;
    } else {
        return FALSE;
    }
}

如果没有,则进行重定向。

if (! is_https()) {
    header("location: https://{$_SERVER['HTTP_HOST']}");
}

关于php重定向代码http到https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29045746/

相关文章:

php - 应用程序中的夜间模式选项,可自动降低屏幕亮度。它会使用一些 php 或 html 或 css 代码来锻炼吗?

php - 设置默认 Controller 或从 url zend 框架中排除某些内容

linux - 如何grep curl -I header信息

c - 编写自己的 shell - 处理某些管道时代码挂起 - 在 C 中

javascript - 如何从默认主页重定向到索引页?

php - 通过管道 $_POST 到外部命令

php - 优化集合包含查询

php - 何时使用 transient ,何时不使用?

.htaccess 重定向后隐藏子目录 url

javascript - 如果您尝试在表单提交之前重定向,会发生什么情况?