【问题标题】:zend https redirect all requestszend https 重定向所有请求
【发布时间】:2012-06-25 01:57:20
【问题描述】:

我想将所有请求重定向到我的 zend Web 应用程序到 https url 我在 .htaccess 文件中尝试了这个并且得到了一个无限重定向

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

有什么想法吗?我已经尝试了几件事,但到目前为止没有一个能始终如一地工作。

【问题讨论】:

  • 看起来问题在于服务器位于负载均衡器后面(我们的登台环境使用 phpfog),因此 ssl 在负载均衡器处终止,这会导致无限重定向。有没有办法检查 HTTP_X_FORWARDED_PORT 信息以避免继续重定向?

标签: php zend-framework .htaccess https apache2


【解决方案1】:

如果我复制你的规则,我似乎不会以无限重定向结束,它工作正常。

这是一个 ZF 插件,我用来强制 https,而不是使用 .htaccess。你可以试试这个,看看它是否有效:

<?php

class Application_Plugin_SslCheck extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $sslModules = array('admin', 'default'); // modules that require ssl
        $module     = $request->getModuleName();

        if (in_array($module, $sslModules)) { // require SSL
            if (APPLICATION_ENV == 'production') { // only require ssl in production mode
                if (!isset($_SERVER['HTTPS']) || !$_SERVER['HTTPS']) { // if request is not secure, redirect to secure url
                    $request    = $this->getRequest();

                    $url        = 'https://'
                                . $_SERVER['HTTP_HOST']
                                . $request->getRequestUri();

                    $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
                    $redirector->gotoUrl($url);
                }
            }
        }
    }
}

要运行它,只需在引导程序中将其注册到前端控制器即可:

Zend_Controller_Front::getInstance()->registerPlugin(new Application_Plugin_SslCheck());

【讨论】:

    猜你喜欢
    • 2016-10-23
    • 2013-04-06
    • 2018-09-15
    • 2013-11-26
    • 2014-06-30
    • 2017-04-30
    • 2014-11-17
    • 2017-11-05
    • 1970-01-01
    相关资源
    最近更新 更多