通过.htaccess强制特定页面上的非SSL(http)和所有其他页面上的SSL

时间:2014-04-11 13:15:23

标签: http zend-framework ssl https

我正在使用Zend-Framework,下面是我的htaccess的代码。

rewriteEngine On

RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.mysite.co/$1 [R=301,L] 
RewriteCond %{http_host} ^mysite.co [NC]
RewriteRule ^(.*)$ https://www.mysite.co/$1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

现在我想在所有其他页面上强制使用mysite.co/news/*和https上的http。那么我该如何申请规则?

2 个答案:

答案 0 :(得分:1)

您可以定义控制器和操作,并将此代码置于该条件中,因为您可以使用zend框架的引导程序。

喜欢:

if($_SERVER['SERVER_PORT'] == '443') {
    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    exit();
}

或者您可以使用此引导方法

protected function _initForceSSL() {
    if($_SERVER['SERVER_PORT'] == '443') {
        header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
        exit();
    }
}

答案 1 :(得分:1)

存在isSecure()功能,可让您知道它是 https 请求( true )还是 http (<强>假)。
您可以尝试通过这样的插件重定向您的查询:

class Application_Plugin_SSL extends Zend_Controller_Plugin_Abstract
{

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request){

      if (!$request->isSecure()){ // -> not https

            $request->setModuleName('yourmodule')
                    ->setControllerName('yourcontrolle')
                    ->setActionName('youraction')
                    ->setDispatched(true) ;
      }
    }
}