The X-Frame-Options HTTP header is used to control whether a browser should be allowed to render a page in a ,
X-Frame-Options Header Values
- DENY: The page cannot be displayed in a frame, regardless of the site attempting to do so.
- SAMEORIGIN: The page can only be displayed in a frame on the same origin as the page itself.
- ALLOW-FROM uri: The page can only be displayed in a frame on the specified origin (note that this is not widely supported by all browsers).
Error Explanation
When you see the error message:
Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
It means that the page you are trying to display in an
For example, if your page is hosted at https://example.com, it can only be embedded in an
Solutions
Ensure Same Origin: Make sure that the page embedding the
- https://yourdomain.com/page1 can embed https://yourdomain.com/page2.
Modify X-Frame-Options Header (if you control the server): If you have control over the server hosting the page, you can modify the X-Frame-Options header to ALLOW-FROM with a specific URL (if supported by the target browsers) or DENY if you want to completely disallow embedding.
X-Frame-Options: ALLOW-FROM https://anotherdomain.com
However, note that ALLOW-FROM is not supported by all browsers (e.g., Chrome and Safari do not support it).
Use Content Security Policy (CSP): A more flexible way to control framing is to use the Content Security Policy header with the frame-ancestors directive, which allows you to specify one or more origins that are allowed to frame the content.
Content-Security-Policy: frame-ancestors 'self' https://anotherdomain.com
This method is widely supported and provides more fine-grained control compared to X-Frame-Options.
Example of Setting CSP Header in Apache:
Header set Content-Security-Policy "frame-ancestors 'self' https://anotherdomain.com"
add_header Content-Security-Policy "frame-ancestors 'self' https://anotherdomain.com";
Conclusion
The X-Frame-Options header is a useful security feature to prevent clickjacking. To resolve the issue of displaying content in an