通过.htaccess在特定页面上强制使用非SSL(http),并在所有其他页面上使用SSL。

5
我正在使用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]

现在我想强制使用http://mysite.co/news/* ,并在所有其他页面使用https://。那么我该如何应用规则呢?

1
只是想指出,当客户端处于从 https:// 重定向到 http:// 的位置时,它已经发出了 HTTPS 请求,因此握手(SSL/TLS 的“昂贵”部分)已经完成。如果您试图实现的是优化,那么我不确定您是否能在此之后实现它。 - Bruno
2个回答

1
你可以定义控制器和操作,并将此代码放入该条件中,为此,您可以使用Zend框架的引导程序。
例如:
if($_SERVER['SERVER_PORT'] == '443') {
    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    exit();
}

您可以使用这个bootstrap方法。
protected function _initForceSSL() {
    if($_SERVER['SERVER_PORT'] == '443') {
        header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
        exit();
    }
}

1

存在一个名为isSecure()的函数,可以让你知道它是一个https请求(true)还是http(false)。
您可以尝试通过这样的插件重定向您的查询:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接