0

我有一个托管在由 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);
}

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

4

1 回答 1

0

这是针对其他问题,如果遇到相同的问题,我通过在beforeFilter()“AppController”类的方法中添加以下代码解决了强制重定向到 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;
    }
于 2020-04-26T21:05:21.270 回答