Setting/Override x-frame-option for only for a specific page on Magento 2.1

Setting/Override x-frame-option for only for a specific page on Magento 2.1

October 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Setting/Override x-frame-option for only for a specific page on Magento 2.1

Setting/Overriding X-Frame-Option for a Specific Page on Magento 2.1

Magento 2.1 sets the X-Frame-Option header to SAMEORIGIN for all frontend pages out of the box, which means it allows embedding only within the same origin. While you can change this setting globally in the app/etc/env.php configuration file, doing so affects all frontend pages.

In your case, you want to override the X-Frame-Option header for a specific page, such as allowing embedding from a different domain for a tracking pixel on one particular page while keeping it secure for other pages. However, setting this directly in the controller doesn’t work because Magento later overrides it using a plugin mechanism.

I’ve successfully achieved this by creating a similar plugin approach to how the Varnish module sets its headers. Here’s a step-by-step guide:

1. Create a new Plugin

The first step is to create a custom plugin that will override the X-Frame-Option header for a specific request.

<?php
namespace Nei\MyModule\Plugin;

use Nei\MyModule\Model\Config;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\Http;

class AllowOriginHeaderForTrackingPlugin
{
    private $config;
    private $request;

    public function __construct(
        Config $config,
        RequestInterface $request
    ) {
        $this->config = $config;
        $this->request = $request;
    }

    /**
     * Magento by default overrides the X-Frame-Option header after Dispatcher.
     *
     * @param Http $subject
     * @return void
     */
    public function beforeSendResponse(Http $subject)
    {
        if ($frontName === 'something' && $actionName === 'anything') {
            $xFrameOption = $this->config->getXFrameOption();
            if (!empty($xFrameOption) && $this->config->isTrackingEnabled()) {
                $subject->setXFrameOptions('ALLOW-FROM '.$xFrameOption);
            }
        }
    }
}

Replace 'something' and 'anything' with the appropriate frontend name and action name you want to target.

2. Configure the Plugin

Next, register your plugin using di.xml. This tells Magento where and when to apply your plugin:

<!-- Nei_MyModule/etc/frontend/di.xml -->
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\Response\Http">
            <plugin name="nei-mymodule-allow-origin" type="Nei\MyModule\Plugin\AllowOriginHeaderForTrackingPlugin"/>
    </type>
</config>

3. Update the Configuration

Ensure you have the necessary configuration settings in your config.xml file:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <nei_mymodule>
            <x_frame_option>https://tracking.example.com</x_frame_option>
            <tracking_enabled>true</tracking_enabled>
        </nei_mymodule>
    </default>
</config>

4. Update the Model

Create a model to handle the configuration values:

<?php
namespace Nei\MyModule\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class Config
{
    private $scopeConfig;

    public function __construct(
        ScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
    }

    public function getXFrameOption()
    {
        return $this->scopeConfig->getValue('nei_mymodule/x_frame_option', ScopeInterface::SCOPE_STORE);
    }

    public function isTrackingEnabled()
    {
        return (bool) $this->scopeConfig->getValue('nei_mymodule/tracking_enabled', ScopeInterface::SCOPE_STORE);
    }
}

Conclusion

While this approach achieves your desired result, it adds some overhead to every frontend response because the plugin is applied on each request. For most cases, especially with a small number of pages like tracking pixels, this should not have a significant impact.

However, if you’re dealing with many pages or performance-critical scenarios, consider looking into Magento’s Page Cache or Varnish cache for more efficient handling. You can also explore the Magento\Framework\App\Http\Context for setting request-specific headers without using a plugin.

Remember to thoroughly test your implementation and monitor performance in a staging environment before deploying it to production.

For more advanced customization options, I recommend checking out Laravel Company’s Magento 2 development guides for best practices and deep dives into Magento architecture.

Magento 2 Cache Security Extensions

Our network