【问题标题】:cakephp2 force https redirect on windows servercakephp2 在 Windows 服务器上强制 https 重定向
【发布时间】:2020-04-25 22:06:24
【问题描述】:

我有一个托管在由 cakephp2.7 开发的 windows 服务器上的子域 在服务器上安装 SSL,如果显式键入 https 但不会自动将 http 转换为 https,则它可以工作 我想自动将任何 url 转换为 https

用 .htaccess 文件尝试了很多东西,但没有任何效果 当前htaccess

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteCond %{HTTPS} !on
 RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f    
 RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

也试过

public $components = array('Security');

public function beforeFilter() {
    $this->Security->blackHoleCallback = 'forceSSL';
    $this->Security->requireSecure();
}

// Add this function in your AppController
public function forceSSL() {
    return $this->redirect('https://' . env('SERVER_NAME') . $this->here);
}

使用此代码,即使任何链接点击都不起作用。

【问题讨论】:

    标签: .htaccess https windows-server-2012 cakephp-2.7


    【解决方案1】:

    如果遇到同样的问题,这是给其他人的 我通过在 `AppController' 类的 beforeFilter() 方法中添加以下代码解决了强制重定向到 https 的问题

        //https redirect
        if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off") {
            $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            header('HTTP/1.1 301 Moved Permanently');
            header('Location: ' . $location);
            exit;
        }
    

    【讨论】:

      猜你喜欢
      • 2017-09-08
      • 2016-12-27
      • 1970-01-01
      • 2016-12-30
      • 2016-11-14
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多