Redirecting HTTPS to HTTP on Non-Secure Magento Pages
Magento is designed to enforce secure communication for critical pages like checkout and admin sections through redirects from HTTP to HTTPS. However, the platform lacks built-in functionality to redirect non-secure pages requested via HTTPS back to HTTP. In this post, we’ll explore why this is important and provide a step-by-step solution to achieve this.
Why Redirect Non-Secure Pages?
Redirecting secure requests for non-secure content serves two primary purposes:
-
Performance Optimization: Serving secure (HTTPS) content requires additional processing compared to HTTP. For non-sensitive pages like category or product listings, serving them over HTTP can improve load times and reduce server resource usage.
-
SEO Benefits: Search engines prefer consistent URL protocols. If your site has a mix of HTTP and HTTPS URLs for the same content, it can create confusion that hurts SEO performance and link integrity.
Identifying the Redirect Issue
When you request an insecure page like https://www.mysite.com/mycategory, Magento’s core logic redirects it to the corresponding HTTP URLâhttp://www.mysite.com/mycategory. This happens because the Magento system assumes all content should be served over HTTPS, which isn’t always optimal.
Understanding the Core Code
The redirect behavior is primarily controlled by two files in the lib/Varien/Http/Adapter.php and lib/Zend/Controller/Front.php classes. Specifically, the getRedirectUrl() method in Zend_Controller_Front.php.
public function getRedirectUrl($url)
{
if ($this->_request->isSecure()) {
$baseUrl = rtrim($this->_baseUrl . '/', '/');
return rtrim(str_replace('http://', 'https://', $baseUrl), '/') . '/' . ltrim($url, '/');
}
return $this->_baseUrl . '/' . ltrim($url, '/');
}
In this code snippet, if the current request is secure (HTTPS), it replaces http:// with https://. This is where the redirect from HTTP to HTTPS occurs.
Implementing a Custom Solution
To redirect non-secure pages requested via HTTPS back to HTTP, we’ll create an observer that intercepts the secure request and modifies the URL accordingly. Here’s the step-by-step process:
- Create a new module with a front controller event observer.
- Define the observer method in your
config.xmlfile:
<events>
<controller_front_init_router>
<observers>
<your_module_controller_front_init_router>
<type>singleton</type>
<class>Your_Module_Model_Observer</class>
<method>redirectNonSecurePages</method>
</your_module_controller_front_init_router>
</observers>
</controller_front_init_router>
</events>
- Implement the observer method in
Observer.php:
public function redirectNonSecurePages($observer)
{
$action = $observer->getEvent()->getAction();
$request = Mage::app()->getRequest();
if ($request->isSecure() && !$this->_isCriticalPage($action)) {
$url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB, true) . $request->getPathInfo();
$request->setPathInfo($url);
}
}
private function _isCriticalPage($action)
{
return in_array($action->getFullActionName(), array('checkout_index_onepage', 'adminhtml_index_index'));
}
- Enable the module configuration.
Conclusion
Redirecting non-secure pages requested via HTTPS back to HTTP can significantly improve performance and SEO for your Magento site. By understanding the core redirect logic and implementing a custom observer, you can achieve this functionality without modifying the core files directly.
Remember, it’s crucial to thoroughly test any URL redirects after implementation to ensure they don’t negatively impact user experience or SEO efforts.
Don’t forget to check out our comprehensive guide on Magento Performance Optimization for more best practices and techniques to enhance your store’s performance.
Happy coding! Let us know if you have any further questions.