【发布时间】:2011-08-18 09:03:37
【问题描述】:
最近,我的完整网站在 iframe 中被另外两个域调用。我想阻止其他试图在 iframe 中显示我的网站的网站。
如何通过 .htaccess 阻止它?
【问题讨论】:
最近,我的完整网站在 iframe 中被另外两个域调用。我想阻止其他试图在 iframe 中显示我的网站的网站。
如何通过 .htaccess 阻止它?
【问题讨论】:
你不能“强制”它,因为有很多方法,但你可以使用标准的标题方法。 html5-boilerplate 有一个不错的 vhost/htaccess sn-p,它首先将 X-Frame-Options 设置为您选择的 DENY/SAMEORIGIN/ALLOW-FROM,然后允许将 MIME 类型列入白名单,以便在 Google 图片搜索等良好框架中使用。
查看最新链接,但这里是 2016 年 1 月 25 日SAMEORIGIN 模式下的示例:
<IfModule mod_headers.c>
Header set X-Frame-Options "SAMEORIGIN"
<FilesMatch "\.(appcache|atom|bbaw|bmp|crx|css|cur|eot|f4[abpv]|flv|geojson|gif|htc|ico|jpe?g|js|json(ld)?|m4[av]|manifest|map|mp4|oex|og[agv]|opus|otf|pdf|png|rdf|rss|safariextz|svgz?|swf|topojson|tt[cf]|txt|vcard|vcf|vtt|webapp|web[mp]|webmanifest|woff2?|xloc|xml|xpi)$">
Header unset X-Frame-Options
</FilesMatch>
</IfModule>
【讨论】:
你可以像下面这样使用.htaccess
RewriteEngine On
RewriteCond %{QUERY_STRING} !^id=[^&]+ [NC]
# if referrer is bad.com
RewriteCond %{HTTP_REFERER} (www\.)?bad\.com [NC]
# then redirect to a different page
RewriteRule !^404.shtm [L,NC,R=302]
您需要依赖 HTTP_REFERER,因为此代码会将来自 bad.com 的所有请求重定向到未找到的页面
这个解决方案促成了这个答案
【讨论】:
您可以在标头 X-Frame-Options: Deny 中设置变量。
所有现代浏览器都支持 X-Frame-Options 标头。
Facebook 使用此标头禁用 iframe/framesets(还有 Javascript)。
如果您在 apache 中启用了 mod_headers:
.htaccess
Header set X-Frame-Options DENY
但是,您可以启用 iframe 来自同一来源。
Header always append X-Frame-Options SAMEORIGIN
或者在 Nginx 中:
add_header X-Frame-Options Deny; #or SAMEORIGIN
浏览器兼容性:Source
【讨论】:
我认为你不能通过 .htaccess,但是你可以使用 JS。您可以使用这样的功能来检查:
function parentIsSameOrigin()
{
var result = true;
if (window.parent)
{
result = Boolean
(
// more precise modifications needed here
window.this.location.href.indexOf(window.parent.location.href) == 0
);
}
return result;
}
【讨论】:
嗯,我不认为你可以。
iframe 是客户端容器,这意味着最终用户的浏览器负责加载 iframe 中的内容。您将无法区分您的页面是否加载到 iframe 中。
【讨论】:
top。 window.top === window.