• 强制使用https的apache设置

    Posted on 十月 11th, 2008 inetdemon No comments

    如果我们有一个域,比如login.abc.com, 这个域我们同时接受http和https协议,但是我们希望将http协议的用户重定向到https,可以有两种做法,一种是在代码中进行重定向,zf的代码如下:

    class Common_Helper_ForceHTTPS extends Zend_Controller_Action_Helper_Abstract {
      public function direct() {
        if (! isset ( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS']) {
          $request = $this->getRequest ();
          $url = 'https://' . $_SERVER ['HTTP_HOST'] . $request->getRequestUri ();
          $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper ( 'redirector' );
          $redirector->gotoUrl ( $url );
        }
      }
    }

    还有一种是进行apache配置,由于apache的配置是c代码,效率较高,我们建议用apache的配置来实现。
    <IfModule mod_rewrite.c>
        RewriteEngine on
        Options +FollowSymLinks
        RewriteBase /   
    RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d   
        RewriteRule ^(.*)$ index.php/$1
       
        RewriteCond $1 !^(index.php|img|css|js|secure_checkout|common_funcs.js|robots.txt)
        RewriteCond %{SERVER_PORT} 80
        RewriteRule /secure_checkout(.*)$ https://www.myURL.com/secure_checkout/$1
    </IfModule>
    RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d   
        RewriteRule ^(.*)$ index.php/$1
       
        RewriteCond $1 !^(index.php|img|css|js|secure_checkout|common_funcs.js|robots.txt)
        RewriteCond %{SERVER_PORT} 80
        RewriteRule /secure_checkout(.*)$ https://www.myURL.com/secure_checkout/$1
    </IfModule>

    Comments are closed.