Hosting

Magento Staging Setup Accessing Live Database with SSL Certificate

Saturday, June 22nd, 2013

With a recent project, we needed to get the Magento staging environment on the same server and accessing the same database as the live environment.  There was lots of setup, product adjusting, etc. that needed to be done, and the client didn’t want to have to do it twice.

Setting up the staging environment was fairly straightforward, thanks to the excellent article over at Mag Life.  However, the article didn’t quite get the job done.

The live site, like all good live ecommerce sites should, was using an SSL certificate to provide a secure connection.  However, the SSL was for a fully qualified domain name (www.sitename.com) and the staging site was on a subdomain (dev.sitename.com).  Because the SSL settings are in the database, every time we attempted to hit a secure page, the dev site would no longer load, and instead would attempt to load the live site again.

Additionally, I felt lazy and didn’t want to set up server environment variables like the article at Mag Life, so I made a few other alterations to the code.

When attempting to set this up, follow the directions over at Mag Life, but utilize this code in your Store.php file:

class Alphachannel_Core_Model_Store extends Mage_Core_Model_Store
{

    /**
     *  This function is modified per the Mag Life article.
     *  With one modification to avoid setting server environment variables
     **/
    public function getBaseUrl($type=self::URL_TYPE_LINK, $secure=null)
    {
    	// This is the lazy bit.  Rather than setting 
        // environment variables, just set the url's here
    	$urls = array("www.sitename.com"=>"dev.sitename.com");
        $store_code = $this->getCode();
        $url = parent::getBaseUrl($type, $secure);
        // Several nested ifs are removed from Mag Life version 
        // that deal with the environment variables
        $host = parse_url($url, PHP_URL_HOST);
        if (isset($urls[$host]))
        {
            $url = str_replace('://'.$host.'/', '://'.$urls[$host].'/', $url);
            $url = str_replace("https:", "http:", $url);
                }
        return $url;
    }

    /**
     * This is the same function as the core files, with one 
     * minor alteration to prevent https redirects
     **/
    public function isCurrentlySecure()
    {
        $standardRule = !empty($_SERVER['HTTPS']) && 'off' != $_SERVER['HTTPS'];
        $offloaderHeader = trim(Mage::getStoreConfig('web/secure/offloader_header'));

        if ((!empty($offloaderHeader) && !empty($_SERVER[$offloaderHeader])) || $standardRule) {
            return true;
        }

        if (Mage::isInstalled()) {
            $secureBaseUrl = Mage::getStoreConfig('web/secure/base_route_url');
            if ( ! $secureBaseUrl) 
            {
                // Here's our hack.  Lie to tell it is secure!
            	return true;
                return false;
            }
            $uri = Zend_Uri::factory($secureBaseUrl);
            $isSecure = ($uri->getScheme() == 'https' )
                && isset($_SERVER['SERVER_PORT'])
                && ($uri->getPort() == $_SERVER['SERVER_PORT']);
            return $isSecure;
        } else {
            $isSecure = isset($_SERVER['SERVER_PORT']) && (443 == $_SERVER['SERVER_PORT']);
            return $isSecure;
        }
    }
}

And, for a little icing on this cake, check out the simple debugging script I whipped up that made it fast and easy to trace back which class / method is calling the current function: Get the calling function or class method

Free Services We Perform

Wednesday, June 1st, 2011

As a part of our full-service approach to every customer, we include many services at no extra charge:
Read More →

Hostgator Continues to be my Favorite

Wednesday, March 2nd, 2011

When it comes to web hosting, I’ve got experience with a bunch of them – GoDaddy, Dreamhost, Network Solutions, 1and1, iPage, NetFirms, WestHost, and many others. I know that GoDaddy is hugely popular, and I also know that while they do many things well, the also have some serious limitations. I’ve moved all of my hosting to Hostgator, and I encourage all of my clients to move their hosting to Hostgator also. Support: awesome. Speed: excellent. Flexibility: outstanding. Ease-of-use: wonderful. Some day I should do a side-by-side comparison, but for now, suffice it to say that I can’t encourage you enough to use Hostgator.