【问题标题】:Redirecting from HTTPS to HTTP从 HTTPS 重定向到 HTTP
【发布时间】:2017-10-06 21:59:32
【问题描述】:

webapp 是使用 CodeIgniter 构建的,我使用的是 Cloudflare 的 Flexible SSL。

页面通过 HTTPS 加载,但只要有重定向,页面就会通过 HTTP 加载。从那时起,所有页面都通过 HTTP 提供。

这是一个例子:

public function logout()
{
    $this->session->sess_destroy();
    redirect('/');

}

注销功能后,所有页面都通过 HTTP 加载。

如何确保页面在重定向后通过 HTTPS 加载?

这是我的 .htaccess 文件的内容:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
 <IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule> 

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.myurl.com/$1 [R,L]

【问题讨论】:

  • 删除第二个RewriteEngine On 行并将建议的条件/规则放入&lt;IfModule mod_rewrite.c&gt; 块中。还要检查 APPPATH.'config/config.php' 基本网址的值并将 https 也放在那里。
  • 为了清楚起见,base url 的值应该是https://www.myurl.com,对吗?
  • 用 php &lt;script&gt;window.location = "http://www.yoururl.com";&lt;/script&gt;987654327@ 的 javascript 重定向
  • https://www.myurl.com/
  • @Tpojka 通过保持我的 .htaccess 文件不变,但将我的配置中的基本 URL 更改为 https,该问题已得到解决。谢谢你的建议

标签: php codeigniter ssl https cloudflare


【解决方案1】:

简单的方法可能是像这样设置 base_url

$config['base_url'] = "https://yoursite.com/";

这将导致所有访问 $config['base_url'](包括 redirect())的 CI 函数使用 https。

不常谈论的是base_url() 通过使用第二个参数来强制协议的能力。例如

echo base_url("blog/post/123", 'https'); 

您可以使用 .htaccess 强制重定向到 https 协议。 把它放在你现有的命令之上。

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

【讨论】:

    猜你喜欢
    • 2019-09-10
    • 2017-11-04
    • 2016-01-17
    • 2011-09-28
    • 2017-07-18
    • 2018-04-09
    • 2019-07-21
    • 1970-01-01
    相关资源
    最近更新 更多