gpt4 book ai didi

php - 从 HTTP 重定向到 HTTPS(最好的方法是 .htaccess 或 PHP 重定向代码)

转载 作者:太空宇宙 更新时间:2023-11-03 13:57:15 26 4
gpt4 key购买 nike

我需要将我的网站链接从 http 重定向到 https
在谷歌上搜索后,我找到了要插入 .htaccess 文件的代码

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

但不幸的是,.htaccess 文件隐藏在我的托管 cPanel 中,无法重写。
所以我发现了以下用于从 http 重定向到 https 的 PHP 代码(作为 .htaccess 的替代)

// Redirection from http to https
if($_SERVER["HTTPS"] != "on") {
header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
exit();
}

我在每个页面上都放置了上面的代码,它可以完美地重定向。

我想知道他们使用 php 代码而不是 .htaccess 来从 http 重定向到 https 有什么缺点。

最佳答案

方案一(编辑.htaccess文件)

要查看 .htaccess 文件,请尝试执行以下操作

  • 转到控制面板
  • 点击文件管理器
  • 选择隐藏文件
  • 选择 .htaccess 文件并点击编辑

enter image description here

方案二(使用Cloudflare.com)

如果您的域链接到 Cloudflare,请使用 Cloudflare https 重定向规则

关于php - 从 HTTP 重定向到 HTTPS(最好的方法是 .htaccess 或 PHP 重定向代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59158826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com