X-Frame-Options SAMEORIGIN blocking iframe on my domain

X-Frame-Options SAMEORIGIN blocking iframe on my domain

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

X-Frame-Options SAMEORIGIN blocking iframe on my domain

You are encountering a classic content security dilemma: you want to embed dynamic content from your own application into an iframe (like Colorbox), but the browser is rejecting it based on the X-Frame-Options header. The error message "Refused to display document because display forbidden by X-Frame-Options" is unambiguous, but the fix requires understanding how headers propagate and where they can be overridden.

The issue stems from a fundamental misunderstanding of what Header always append X-Frame-Options SAMEORIGIN actually achieves in your specific context. By appending this header to your root .htaccess, you are telling browsers that any resource served from your domain can only be framed by other resources on the exact same origin. While this prevents clickjacking attacks where a malicious third party forces your site into their frame, it simultaneously blocks legitimate iframe embedding if the browser receives conflicting signals or if the header is set on the wrong endpoint.

If you are using Colorbox to load an external URL, that specific URL must send the correct headers, not necessarily your root domain. However, if you are attempting to load a page from your own subdirectory (e.g., yoursite.com/gallery into yoursite.com/lightbox) and it fails, the problem usually lies in how the server constructs the response or how the application framework handles header injection.

The Precedence of Header Directives

The .htaccess file operates at the Apache server level. When you use Header always append, you are instructing the server to add the header to any existing headers already set by other modules or scripts before the final response is sent. This is useful for layering security policies, but it does not guarantee that your specific intent will survive if another piece of code explicitly sets a conflicting value earlier in the request lifecycle.

In many modern PHP applications, frameworks or libraries often set their own security headers automatically. If your application framework (or a library included by it) is configured to deny framing globally, an append directive in .htaccess might not override that explicit setting. To verify this, you need to inspect the full response header dump in your browser's developer tools. Look specifically for any X-Frame-Options: DENY or X-Frame-Options: SAMEORIGIN headers that appear before your appended value, although typically the last header wins in standard HTTP behavior.

Application-Level Overrides

You asked if code can override .htaccess. The answer is a definitive yes. Apache processes directives in a specific order, and application-level PHP scripts execute after the server has parsed static configuration files. If your application includes code like this:

header('X-Frame-Options: SAMEORIGIN');

or uses middleware that sets Content Security Policy headers including frame-ancestors, these will take precedence over or interact complexly with your .htaccess rules depending on the specific Apache module version and load order.

Furthermore, if you are using a framework like Laravel, there is a dedicated configuration option for this exact scenario. The application might be enforcing strict framing policies via App\Http\Middleware\TrustProxies or specific CSP middleware that overrides server-level headers. In Laravel 5.4 and later, you can manage this via the config.csp.php file or by adjusting the headers array in your app/Http/Middleware/TrustProxies configuration, though direct header manipulation is often preferred for granular control over specific views.

The Correct Approach for Same-Origin Iframes

If your goal is to allow an iframe on your own domain but restrict third parties, SAMEORIGIN is the correct directive. However, if you are trying to load a page from yoursite.com/page1 into yoursite.com/page2, both pages must agree. If page1 returns X-Frame-Options: DENY (which some older CMS themes or plugins do by default), your .htaccess append will fail because the browser sees a hard refusal.

To resolve this without modifying the root .htaccess globally, which might affect other parts of your legacy application, you should target the specific response generating that content. If you have access to the PHP code rendering the embedded page, ensure no conflicting headers are set there. Alternatively, consider using X-Frame-Options ALLOW-FROM (deprecated but historically relevant) or moving to modern Content Security Policy (CSP).

The CSP approach offers more flexibility. Instead of relying solely on X-Frame-Options, you can define a frame-ancestors meta tag or HTTP header in your HTML:

<meta http-equiv="Content-Security-Policy" content="frame-ancestors 'self'">

This is functionally equivalent to SAMEORIGIN but allows for finer grain control if you need to allow specific domains later. If you are using a framework like Laravel, the LaravelCompany documentation provides excellent patterns for managing these security headers without breaking existing legacy integrations. They emphasize that while .htaccess is powerful, application-level configuration often yields cleaner separation of concerns in modern stacks.

Ultimately, if you cannot modify the source code of the page being embedded, and it is serving a hard DENY, there is no server-side workaround. You must coordinate with the owner of that specific resource to update their headers to permit framing from your domain.

Our network