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

通过.htaccess强制特定页面上的非SSL(http)和所有其他页面上的SSL,http,zend-framework,ssl,https,Http,Zend Framework,Ssl,Https,我正在使用Zend框架,下面是我的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_FILE

我正在使用Zend框架,下面是我的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/*上强制使用http,在所有其他页面上强制使用https。那个么我该如何应用规则呢?

你们可以定义你们的控制器和动作,并把这些代码放到那个条件下,因为你们可以使用zend framework的引导

比如:

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();
    }
}

存在
isSecure()
函数,可以让您知道它是https请求(true)还是httpfalse)。
您可以尝试通过以下插件重定向查询:

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) ;
      }
    }
}

需要指出的是,当客户端能够从
https://
重定向到
http://
时,它已经发出了https请求,因此握手(SSL/TLS的“昂贵”部分)已经发生了。如果这是你想要做的,我不确定你会得到你想要达到的优化。