Solving HTTPS Image Link Issues on Magento
As an experienced e-commerce developer, I often encounter challenges related to secure URLs when working with Magento. One of the most common issues is when images in static blocks appear as insecure HTTP links even after enabling HTTPS.
Understanding the Issue
When you enable HTTPS for your Magento site, it’s crucial that all linked content also uses this secure protocol. However, if you’ve inserted media files using the {{media url="...."}} syntax within a static block, these links may remain in their original HTTP format even after the transition to HTTPS.
Why It Happens
This occurs because Magento’s core {{media}} tag still generates absolute URLs based on the store’s unsecured base URL. The system doesn’t automatically convert them when you switch your site to use SSL.
A Practical Solution
To resolve this issue without manually editing hundreds of static blocks, I recommend using a third-party extension or implementing a custom solution. Here are two approaches:
1. Using an Extension: Magento HTTPS Image Fix
One reliable option is the “Magento HTTPS Image Fix” extension available on Laravel Company’s Magento Extensions page.
This extension automatically replaces all HTTP image links with their corresponding secure HTTPS versions site-wide, including those in static blocks. It’s a quick and efficient solution for this specific problem.
// Example configuration (magento-https-image-fix.xml)
<config>
<default>
<https_image_fix>
<enable>1</enable>
//... other settings...
</https_image_fix>
</default>
</config>
2. Custom Solution: Magento’s Rewrite System
If you prefer a custom solution, you can use Magento’s rewrite system to replace the problematic HTTP links with their HTTPS counterparts.
To do this, create an observer that listens for the core_block_abstract_to_html_before event and manually rewrite the URLs as needed:
// Observer class (RewriteHttpsImagesObserver)
public function aroundRenderLayout($observer) {
$output = $observer->getOutput();
$httpsUrl = str_replace('http://', 'https://', $observer->getRequest()->getScheme() . '://' . $_SERVER['HTTP_HOST']);
// Regular expression to match and replace http:// domain in img src attribute
$regexp = '/<img[^>]+src=\"([^\"]+)\"/i';
if (preg_match_all($regexp, $output, $matches)) {
foreach ($matches[1] as $url) {
if (strpos($url, 'http://') !== false) {
$newUrl = str_replace('http://', 'https://', $url);
$output = preg_replace('/<img[^>]+src=\"' . $url . '\"/i', '<img src="' . $newUrl . '" />', $output);
}
}
}
return $output;
}
Remember to register this observer correctly in your etc/events.xml file:
<config>
<global>
<events>
<core_block_abstract_to_html_before>
<observers>
<RewriteHttpsImagesObserver>
<type>singleton</type>
<class>Namespace\RewriteHttpsImagesObserver</class>
<method>aroundRenderLayout</method>
</RewriteHttpsImagesObserver>
</observers>
</core_block_abstract_to_html_before>
</events>
</global>
</config>
Conclusion
Resolving HTTPS image link issues on Magento sites is a critical step in ensuring your site’s security and SEO. Whether you choose to use an extension like the “Magento HTTPS Image Fix” or implement a custom solution using observers, addressing this problem will enhance your site’s performance and user experience.
Always remember to thoroughly test any changes made to ensure they don’t negatively impact other aspects of your website. If you need further assistance with Magento development or would like to explore more extensions, feel free to visit Laravel Company.