How to prevent a website from being loaded in an iframe

Today we will show you how to prevent a website from being loaded in an iframe. The X-Frame-Options HTTP response header can be used to block a website from loading in iframe.

There are three different possible values for X-Frame-Options:

  • 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.
  • ALLOW-FROM uri – The page can only be displayed in a frame on the specified origin.

Multiple ways to prevent a website from being loaded in an iframe

  1. Using meta tag
  2. Configuring Apache
  3. Configuring nginx
  4. Configuring IIS
  5. Configuring HAProxy
  6. Configuring Express
  7. Configuring .htaccess

1. Using meta tag

Use the following meta tags to prevent the access.

Deny access from an iframe for all

Deny access from other websites

Note: Setting X-Frame-Options inside the <meta> element is useless! For instance, <meta http-equiv="X-Frame-Options" content="deny"> has no effect. Do not use it! X-Frame-Options works only by setting through the HTTP header, as in the examples below.

2. Configuring Apache

To configure Apache to send the X-Frame-Options header for all pages, add the following code to your site’s configuration.

Use the following code to configure Apache to set the X-Frame-Options DENY.

3. Configuring nginx

Similarly use the following code for the nginx configuration.

4. Configuring IIS

Use the following code to update the configuration for IIS server.

5. Configuring HAProxy

Let’s use the following code to configure HAProxy.

For newer versions

6. Configuring Express

We need to use the helmet to update configuration in the express server.

7. Configuring .htaccess

To manage the same type of configuration in PHP, you can also use the .htaccess file. Add the following code in your file.

After successful configuration, when you load the website into the iframe, you will see the X-Frame-Options as shown in the image below.

Output - How to prevent a website from being loaded in an iframe - Clue Mediator
Output – How to prevent a website from being loaded in an iframe – Clue Mediator

Check this link for more information about the X-Frame-Options.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *