我想将我的 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/