Https request on some pages, but not on all pages of the zend framework

I need to put https on some URL, but not the whole URL. I use the zend URl view assistant for all links. I have an SSL * .example.com certificate for the entire site. Now I open the site with https://www.example.co , then the whole link on the home page or on other pages contains https in the URL. How can I make a specific request on an https address and the other page should open normally.

I also need to transfer some redirection so that if someone opens certain pages in a regular URL, they are redirected to https-url. I think a .htaccess redirect will work for this.

Any help ????

Thanks in advance!

+3
source share
2 answers

I rather used a simple method, and it does not require any changes to the URL generation / routing. I wrote the following lines in the routeStartup function of the plugin and made no changes to the route URL.

public function routeStartup(Zend_Controller_Request_Abstract $request)
{

   $controller=$this->_controller = $this->_front->getRequest()->getControllerName();
   $action=$this->_controller = $this->_front->getRequest()->getActionName();

    if($_SERVER['SERVER_PORT']!=443)
    {
//controller and actions array to check ssl links
        $actionArr=array('index'=>array('registration'),'account'=>array('editacc','edit'),'dealer'=>array('*'));
        if(array_key_exists($controller,$actionArr)!==false)
        {
            if(in_array($action,$actionArr[$controller]) || $actionArr[$controller][0]=='*')
            {
                $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
                 $redirector->gotoUrl(SITE_LIVE_URL_SSL.$controller."/".$action);    
            }
        }


    }
    else
    {
//controller and action array that should not be on ssl.
          $notAactionArr=array('usersearch'=>array('mainserarch'),'account'=>array('index'),'onlineusers'=>array('*'));
        if(array_key_exists($controller,$notAactionArr)!==false)
        {
            if(in_array($action,$notAactionArr[$controller]) || $notAactionArr[$controller][0]=='*')
            {
                $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
                 $redirector->gotoUrl(SITE_LIVE_URL.$controller."/".$action);    
            }
        }

    }
}
+1
source

The ViewHelper URL only collects paths that are absolute on behalf of the host. so you need to explicitly specify the https link prefix

<? $url = $view->url(array('some' => 'params') /*, $route, $reset*/) ?>
<a href="https://<?= $_SERVER['HTTP_HOST] ?><?= $url ?>">my explicit https link</a>

you should create your own small viewhelper that does this for you, and also checks if HTTP_HOST, etc. is set, maybe you also take it from config instead of $ _SERVER.

$ view-> httpsUrl (array ('some' => 'params') /, $ route, $ reset /);

, reqeust https, , , otehr.

:

My_Controller_Plugin_HttpBlacklist extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        // when /foo/bar/baz is requested
        if (($request->getModuleName() == 'foo' &&
            $request->getControllerName() == 'bar' &&
            $request->getControllerName() == 'baz')
            /* || (conditions for more requests)*/) {

            //very basic confifiotn to see if https is enabled, should be done better...
            if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') {

                // should be done with Zend_Http_Reponse instead
                header('Location: https://'. $_SERVER['HTTP_HOST] . $_SERVER['REQUEST_URI']);
                exit;
            }
        }
    }
}

$frontController- > registerPlugin ( My_Controller_Plugin_HttpBlacklist);

+2

Source: https://habr.com/ru/post/1766185/


All Articles