2010-12-17 119 views
1

我開發了一個cakephp站點,它應該對所有頁面使用ssl。它按預期工作,除非在控制器中使用重定向時,它會重定向到http://subdomain.domain.com而不是https://subdomain.domain.com/controller/action。重定向到cakephp ssl站點路由到http而不是https

我已經通過創建端口80指向的CakePHP應用程序的虛擬主機解決了這個和添加在.htaccess

的RewriteCond%{HTTPS}這些重寫規則關閉 重寫規則(。*)https://% {HTTP_HOST}% {REQUEST_URI} [L]

這會捕獲這種情況並重定向到https,但這會給服務器帶來不必要的額外流量。

這個額外的流量的原因是重定向功能,因爲它會產生錯誤的網址。我查看了重定向函數,它調用router :: url來創建實際的url。但我無法弄清楚如何或在何處指示路由器使用https而不是http。

回答

0

我發現它是Apache的錯誤配置的錯誤。

嘗試Jamies解決方案,網站結束了重定向循環,因爲即使請求是https,RequestHandler-> isSSL()也返回false。然後我發現沒有設置$ _SERVER ['https'],$ _SERVER ['port']是80,而不是443。

在這一點上我已經把我的SSL指令在網站可用/默認情況下,

SSLEngine on 
SSLCertificateFile [path to cert file] 
SSLCertificateKeyFile [path to keyfile] 

移動SSL指令爲子域的虛擬主機解決了這個問題,與重定向循環。

其實也解決了我最初的問題,因爲該方法路由器:: URL檢查$ _ SERVER [「HTTPS」如果設置生成以https開頭的URL:否則只是HTTP:

我已經從測試Jameies解決方案和我自己的.htaccess中的重寫規則,他們都在修復後按預期工作。

5

我正在一點猜測,但我懷疑這是這個非常重寫規則多數民衆贊成搞亂的東西了。

你應該「重定向」而不是「重寫」。 Cake的生成鏈接通常是相對於根的,所以除非你傳遞「true」作爲第二個參數,否則不要指定協議。

我也有Apache和80和443聽,這樣我至少可以迴應不正確的請求。

這是我在我的AppController類做同樣的事情代碼:

function beforeFilter() { 
    parent::beforeFilter(); 
    $this->_setupSecurity(); 
} 

function _setupSecurity() { 
    $this->Security->blackHoleCallback = '_badRequest'; 
    if(Configure::read('forceSSL')) { 
     $this->Security->requireSecure('*'); 
    } 
} 

/** 
* The main SecurityComponent callback. 
* Handles both missing SSL problems and general bad requests. 
*/ 

function _badRequest() { 
    if(Configure::read('forceSSL') && !$this->RequestHandler->isSSL()) { 
     $this->_forceSSL(); 
    } else { 
     $this->cakeError('error400'); 
    } 
    exit; 
} 

/** 
* Redirect to the same page, but with the https protocol and exit. 
*/ 

function _forceSSL() { 
    $this->redirect('https://' . env('SERVER_NAME') . $this->here); 
    exit; 
} 

我也有我自己的配置「forceSSL」在bootstrap.php中用於打開這個和關閉取決於選項環境,所以需要將上述設置爲true才能正常工作。

0

其示例在食譜 http://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#usage

class AppController extends Controller { 
    // Add security component 
    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); 
    } 
} 
已經提到
相關問題